PHP code example of izniburak / laravel-auto-routes

1. Go to this page and download the library: Download izniburak/laravel-auto-routes library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

izniburak / laravel-auto-routes example snippets


Route::auto('/test', 'TestController');

// use Illuminate\Support\Facades\Route;
use Buki\AutoRoute\Facades\Route;

Route::auto('/test', 'TestController');
# OR
Route::auto('/test', TestController::class);

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * URL will be converted to "/test/foo-bar"
     */
    public function fooBar(Request $request)
    {
        // your codes
    }
}

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * URL: "/test/foo-bar"
     * This method will only work with 'GET' method. 
     */
    public function getFooBar(Request $request)
    {
        // your codes
    }
    
    /**
     * URL: "/test/bar-baz"
     * This method will only work with 'POST' method. 
     */
    public function postBarBaz(Request $request)
    {
        // your codes
    }
}

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * URL: "/test/foo_bar"
     * This method will only work with 'GET' method. 
     */
    public function get_foo_bar(Request $request)
    {
        // your codes
    }
    
    /**
     * URL: "/test/bar_baz"
     * This method will only work with 'POST' method. 
     */
    public function post_bar_baz(Request $request)
    {
        // your codes
    }
}

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * URL: "/test/foo"
     * This method will only work with 'GET' method and XMLHttpRequest.
     */
    public function xgetFoo(Request $request)
    {
        // your codes
    }
    
    /**
     * URL: "/test/bar"
     * This method will only work with 'POST' method and XMLHttpRequest.
     */
    public function xpostBar(Request $request)
    {
        // your codes
    }
    
    /**
     * URL: "/test/baz"
     * This method will work with any method and XMLHttpRequest. 
     */
    public function xanyBaz(Request $request)
    {
        // your codes
    }
}

Route::auto('/test', 'TestController', [
    // your options... 
]);

Route::auto('/test', 'TestController', [
    'name' => 'test',
    'middleware' => [YourMiddleware::class],
    'patterns' => [
        'id' => '\d+',
        'value' => '\w+',
    ],
]);

# First Example
Route::auto('/foo', 'FooController', [
    'only' => ['fooBar', 'postUpdatePost'],
]);

# Second Example
Route::auto('/bar', 'BarController', [
    'except' => ['test', 'putExample'],
]);

Route::auto('/test', 'TestController');

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * URL: "/test"
     */
    public function index(Request $request)
    {
        // your codes
    }
    
    /**
     * URL: "/test/foo-bar"
     */
    public function fooBar(Request $request)
    {
        // your codes
    }
}

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * URL: "/test/{id}"
     */
    public function index(Request $request, $id)
    {
        // your codes
    }
    
    /**
     * URL: "/test/foo-bar/{name}/{surname?}"
     */
    public function fooBar(Request $request, $name, $surname = null)
    {
        // your codes
    }
}

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * URL: "/test/{id}"
     * id parameter must be numeric.  
     */
    public function index(Request $request, int $id)
    {
        // your codes
    }
    
    /**
     * URL: "/test/foo-bar/{name}/{surname?}"
     * name and surname parameters must be string.
     */
    public function fooBar(Request $request, string $name, string $surname = null)
    {
        // your codes
    }
}

Route::auto('/test', 'Backend.TestController');
# OR
Route::auto('/test', 'Backend\\TestController');

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * URL: "/test/foo"
     */
    public function voltFoo(): string
    {
        // resources/views/livewire/pages/foo.blade.php
        return 'pages.foo';
    }

    /**
     * URL: "/test/bar"
     */
    public function wireBar(): string
    {
        return \App\Livewire\TestComponent::class;
    }
}
sh
php artisan vendor:publish --provider="Buki\AutoRoute\AutoRouteServiceProvider"