PHP code example of annotation / routing

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

    

annotation / routing example snippets


namespace App\Http\Controllers\Backend;

use Annotation\Route\Prefix;

#[Prefix('backend')]
class BaseController extends Controller
{}

namespace App\Http\Controllers\Backend;

use Annotation\Route\Domain;
use Annotation\Route\Group;
use Annotation\Route\Prefix;
use Annotation\Route\Route\Get;

#[Group(prefix: 'home')]
class HomeController extends BaseController
{
    #[Get('index', 'index')]
    public function index(Request $request)
    {
        //
    }
}

use App\Http\Controllers\Backend\HomeController;
use Illuminate\Support\Facades\Route;

Route::prefix('backend/home')
    ->name('backend.home.')
    ->group(function () {
        Route::get('index', [HomeController::class, 'index'])->name('index');
    });

return [

    /*
    |--------------------------------------------------------------------------
    | Automatic Registration Routes
    |--------------------------------------------------------------------------
    |
    | Automatic registration of routes will only happen if this setting is `true`
    |
    */

    'enabled' => true,

    /*
    |--------------------------------------------------------------------------
    | Automatically Registered Paths
    |--------------------------------------------------------------------------
    |
    | Controllers in these directories that have routing attributes will automatically be registered.
    | Optionally, you can specify group configuration by using key/values
    |
    */

    'directories' => [
        app_path('Http/Controllers'),
        app_path('Http/Controllers/Web') => [
            'middleware' => ['web'],
        ],
        app_path('Http/Controllers/Api') => [
            'prefix' => 'api',
            'middleware' => 'api',
        ],
    ],
];

namespace App\Http\Controllers;

use Annotation\Route\Route\Get;

class MyController extends Controller
{
    #[Get('route')]
    public function myMethod()
    {
        //
    }
}

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

namespace App\Http\Controllers\Backend;

use Annotation\Route\Prefix;
use Annotation\Route\Routing\Config;
use App\Http\Controllers\Controller;

#[Prefix('backend')]
#[Config('domains.backend', 'backend.local.dev')]
class BaseController extends Controller
{}

namespace App\Http\Controllers\Backend;

use Annotation\Route\Prefix;
use Annotation\Route\Route\Get;

#[Prefix('home')]
class HomeController extends BaseController
{
    #[Get]
    public function index(Request $request)
    {
    }
}

use App\Http\Controllers\Backend\HomeController;
use Illuminate\Support\Facades\Route;

Route::prefix('backend/home')
    ->name('backend.home.')
    ->domain('backend.local.dev')
    ->group(function () {
        Route::get('index', [HomeController::class, 'index'])->name('index');
    });

namespace App\Http\Controllers\Backend;

use Annotation\Route\Group;
use Annotation\Route\Route\Get;

#[Group(prefix: 'account', domain: 'passport.local.dev', as: 'passport')]
class AccountController extends BaseController
{
    #[Get]
    public function login(Request $request)
    {
    }
}

use App\Http\Controllers\Backend\AccountController;
use Illuminate\Support\Facades\Route;

Route::prefix('backend/account')
    ->name('backend.passport.')
    ->group(function () {
        Route::get('login', [AccountController::class, 'login'])->name('login')->domain('passport.local.dev');
    });

namespace App\Http\Controllers;

use Annotation\Route\Prefix;
use Annotation\Route\Route\Get;

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

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

namespace App\Http\Controllers;

use Annotation\Route\Route\Get;

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

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

#[Annotation\Route\Route\Post('uri')]
#[Annotation\Route\Route\Put('uri')]
#[Annotation\Route\Route\Patch('uri')]
#[Annotation\Route\Route\Delete('uri')]
#[Annotation\Route\Route\Options('uri')]

#[Annotation\Route\Route\Any('uri')]

#[Annotation\Route\Route(['put', 'patch'], 'uri')]

namespace App\Http\Controllers;

use Annotation\Route\Route\Get;

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

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

namespace App\Http\Controllers;

use Annotation\Route\Route\Get;
use Annotation\Route\Middleware;
use Annotation\Route\WithoutMiddleware;

#[Middleware(GlobalMiddleware::class)]
class MyController
{
    #[Get('route', middleware: MyMiddleware::class)]
    public function myMethod()
    {
    }
    
    #[Get('global-middleware')]
    public function globalMiddleware()
    {
    }
    
    #[Get('without-middleware', withoutMiddleware: GlobalMiddleware::class)]
    // or
    // #[WithoutMiddleware(GlobalMiddleware::class)]
    public function withoutMiddleware()
    {
    }
}

Route::get('route', [MyController::class, 'myMethod'])->middleware([GlobalMiddleware::class, MyMiddleware::class]);
Route::get('global-middleware', [MyController::class, 'globalMiddleware'])->middleware(GlobalMiddleware::class);
Route::get('without-middleware', [MyController::class, 'withoutMiddleware']);

namespace App\Http\Controllers;

use Annotation\Route\Domain;
use Annotation\Route\Route\Get;

#[Domain('subdomain.localhost')]
class MyController
{
    #[Get('route')]
    public function myMethod()
    {
    }
}

Route::get('route', [MyController::class, 'myMethod'])->domain('subdomain.localhost');

// config/app.php
return [
    'url' => env('APP_URL', 'http://localhost'),
];

namespace App\Http\Controllers;

use Annotation\Route\Route\Get;
use Annotation\Route\Routing\Config;

#[Config('app.url', '127.0.0.1')]
class MyController
{
    #[Get('route')]
    public function myMethod()
    {
    }
}

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

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

namespace App\Http\Controllers;

use Annotation\Route\Route\Get;
use Annotation\Route\ScopeBindings;

class MyController
{
    #[Get('users/{user}/posts/{post}')]
    #[ScopeBindings]
    public function myMethod(User $user, Post $post)
    {
    }
}

Route::get('users/{user}/posts/{post}', [MyController::class, 'myMethod'])->scopeBindings();

#[Annotation\Route\ScopeBindings(false)]

namespace App\Http\Controllers;

use Annotation\Route\Route\Get;
use Annotation\Route\Route\Post;
use Annotation\Route\Routing\WhereAlphaNumeric;
use Annotation\Route\Where;

#[Where('custom', '[0-9]+')]
class MyController
{
    #[Get('route/{custom}')]
    public function myMethod()
    {
    }

    #[Post('post-route/{custom}/{alpha-numeric}')]
    #[WhereAlphaNumeric('alpha-numeric')]
    public function myPostMethod()
    {
    }
}

Route::get('route/{custom}', [MyController::class, 'myMethod'])->where(['custom' => '[0-9]+']);
Route::post('post-route/{custom}/{alpha-numeric}', [MyController::class, 'myPostMethod'])->where(['custom' => '[0-9]+', 'alpha-numeric' => '[a-zA-Z0-9]+']);

#[Annotation\Route\Routing\WhereAlpha('alpha')]
#[Annotation\Route\Routing\WhereAlphaNumeric('alpha-numeric')]
#[Annotation\Route\Routing\WhereIn('in', ['value1', 'value2'])]
#[Annotation\Route\Routing\WhereNumber('number')]
#[Annotation\Route\Routing\WhereUlid('ulid')]
#[Annotation\Route\Routing\WhereUuid('uuid')]

namespace App\Http\Controllers;

use Annotation\Route\Group;
use Annotation\Route\Route\Get;

#[Group(prefix: 'domain', domain: 'domain.localhost')]
#[Group(prefix: 'subdomain', domain: 'subdomain.localhost')]
class MyController
{
    #[Get('route')]
    public function myMethod()
    {
    }
}

Route::get('route', [MyController::class, 'myMethod'])->prefix('domain')->domain('domain.localhost');
Route::post('route', [MyController::class, 'myMethod'])->prefix('subdomain')->domain('subdomain.localhost');

namespace App\Http\Controllers;

use Annotation\Route\Route\Get;
use Annotation\Route\Route\Post;
use Annotation\Route\Routing\Defaults;

#[Defaults('param', 'default')]
class MyController
{
    #[Get('route/{param?}')]
    public function myMethod($param)
    {
    }

    #[Post('route/{param?}/{param2?}')]
    #[Defaults('param2', 'post-default')]
    public function myPostMethod($param, $param2)
    {
    }

    #[Get('override-route/{param?}')]
    #[Defaults('param', 'override-default')]
    public function myOverrideMethod($param)
    {
    }
}

Route::get('route/{param?}', [MyController::class, 'myMethod'])->setDefaults(['param' => 'default']);
Route::post('route/{param?}/{param2?}', [MyController::class, 'myPostMethod'])->setDefaults(['param' => 'default', 'param2' => 'post-default']);
Route::get('override-route/{param?}', [MyController::class, 'myOverrideMethod'])->setDefaults(['param' => 'override-default']);

namespace App\Http\Controllers;

use Annotation\Route\Route\Get;
use Annotation\Route\Route\Post;
use Annotation\Route\WithTrashed;

#[WithTrashed]
class MyController
{
    #[Get('route')]
    #[WithTrashed]
    public function myMethod()
    {
    }

    #[Post('route')]
    #[WithTrashed(false)]
    public function myPostMethod()
    {
    }

    #[Get('default-route')]
    public function myDefaultMethod()
    {
    }
}

Route::get('route', [MyController::class, 'myMethod'])->WithTrashed();
Route::post('route', [MyController::class, 'myPostMethod'])->withTrashed(false);
Route::get('default-route', [MyController::class, 'myDefaultMethod'])->withTrashed();

namespace App\Http\Controllers;

use Annotation\Route\Fallback;

class MyController
{
    #[Fallback]
    public function __invoke(Request $request)
    {
        return '404';
    }
}

Route::fallback(\App\Http\Controllers\MyController::class)->name('fallback');

namespace App\Http\Controllers;

use Annotation\Route\Prefix;
use Annotation\Route\Resource;
use Illuminate\Http\Request;

#[Prefix('api/v1')]
#[Resource(
    resource: 'photos.comments',
    apiResource: true,
    except: ['destroy'],
    names: 'photo-comments',
    parameters: ['comments' => 'comment:uuid'],
    shallow: true,
)]
class PhotoCommentController
{
    public function index($photo)
    {}

    public function store(Request $request, $photo)
    {}

    public function show($comment)
    {}

    public function update(Request $request, $comment)
    {}
}

Route::get('api/v1/comments/{comment}', [PhotoCommentController::class, 'show'])->name('api.v1.photo-comments.show');
Route::match(['put', 'patch'], 'api/v1/comments/{comment}', [PhotoCommentController::class, 'update'])->name('api.v1.photo-comments.update');
Route::get('api/v1/photos/{photo}/comments', [PhotoCommentController::class, 'index'])->name('api.v1.photo-comments.index');
Route::post('api/v1/photos/{photo}/comments', [PhotoCommentController::class, 'store'])->name('api.v1.photo-comments.store');
shell
php artisan vendor:publish --provider="Annotation\Routing\RouteServiceProvider" --tag="config"