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()
{
//
}
}
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()
{
}
}
namespace App\Http\Controllers;
use Annotation\Route\Route\Get;
class MyController
{
#[Get('route', name: 'my-route')]
public function myMethod()
{
}
}
namespace App\Http\Controllers;
use Annotation\Route\Route\Get;
class MyController
{
#[Get('route', middleware: MyMiddleware::class)]
public function myMethod()
{
}
}
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()
{
}
}
namespace App\Http\Controllers;
use Annotation\Route\Domain;
use Annotation\Route\Route\Get;
#[Domain('subdomain.localhost')]
class MyController
{
#[Get('route')]
public function myMethod()
{
}
}
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()
{
}
}
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)
{
}
}
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()
{
}
}
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()
{
}
}
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)
{
}
}
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()
{
}
}
namespace App\Http\Controllers;
use Annotation\Route\Fallback;
class MyController
{
#[Fallback]
public function __invoke(Request $request)
{
return '404';
}
}
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)
{}
}