PHP code example of bfg / route

1. Go to this page and download the library: Download bfg/route 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/ */

    

bfg / route example snippets


use Bfg\Route\Attributes\Get;
use Bfg\Route\Attributes\Resource;

class MyController
{
    #[Get('my-route')]
    public function myMethod()
    {

    }
}

#[Resource('my_resource')]
class MyResourceController
{
    ...
}

Route::get('my-route', [MyController::class, 'myMethod']);

public function boot()
{
    $this->configureRateLimiting();

    $this->routes(function () {

        Route::find(
            // Path for search attributes,
            // you can use class namespaces,
            // directories and file paths
            __DIR__ . '/../Http/Controllers',
            
            // Here you can transfer the parent
            // instance of the route from which
            // the nesting will be created.
            Route::middleware('web')
        );
    });
}

use Bfg\Route\Attributes\Get;

class MyController
{
    #[Get('my-route')]
    public function myMethod()
    {

    }
}

Route::get('my-route', [MyController::class, 'myMethod']);

#[Bfg\Route\Attributes\Post('my-uri')]
#[Bfg\Route\Attributes\Put('my-uri')]
#[Bfg\Route\Attributes\Patch('my-uri')]
#[Bfg\Route\Attributes\Delete('my-uri')]
#[Bfg\Route\Attributes\Options('my-uri')]

use Bfg\Route\Attributes\Get;

class MyController
{
    #[Get('my-route', name: "my-route-name")]
    public function myMethod()
    {

    }
}

Route::get('my-route', [MyController::class, 'myMethod'])->name('my-route-name');

use Bfg\Route\Attributes\Get;

class MyController
{
    #[Get('my-route', middleware: MyMiddleware::class)]
    public function myMethod()
    {

    }
}

Route::get('my-route', [MyController::class, 'myMethod'])->middleware(MyMiddleware::class);

use Bfg\Route\Attributes\Get;
use Bfg\Route\Attributes\Middleware;

#[Middleware(MyMiddleware::class)]
class MyController
{
    #[Get('my-route')]
    public function firstMethod()
    {
    }

    #[Get('my-other-route', middleware: MyOtherMiddleware::class)]
    public function secondMethod()
    {
    }
}

Route::get('my-route', [MyController::class, 'firstMethod'])->middleware(MyMiddleware::class);
Route::get('my-other-route', [MyController::class, 'secondMethod'])->middleware([MyMiddleware::class, MyOtherMiddleware]);

use Bfg\Route\Attributes\Get;
use Bfg\Route\Attributes\Post;
use Bfg\Route\Attributes\Prefix;

#[Prefix('my-prefix')]
class MyController
{
    #[Get('my-get-route')]
    public function myGetMethod()
    {
    }

    #[Post('my-post-route')]
    public function myPostMethod()
    {
    }
}

Route::get('my-prefix/my-get-route', [MyController::class, 'myGetMethod']);
Route::post('my-prefix/my-post-route', [MyController::class, 'myPostMethod']);

use Bfg\Route\Attributes\Get;
use Bfg\Route\Attributes\Post;
use Bfg\Route\Attributes\Domain;

#[Domain('my-subdomain.localhost')]
class MyController
{
    #[Get('my-get-route')]
    public function myGetMethod()
    {
    }

    #[Post('my-post-route')]
    public function myPostMethod()
    {
    }
}

Route::get('my-get-route', [MyController::class, 'myGetMethod'])->domain('my-subdomain.localhost');
Route::post('my-post-route', [MyController::class, 'myPostMethod'])->domain('my-subdomain.localhost');
bash
php artisan route:cache