PHP code example of ahmednour / laravel-attribute-routing

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

    

ahmednour / laravel-attribute-routing example snippets


#[Prefix('api/leads')]
#[Middleware(['api', 'auth:sanctum'])]
#[Name('leads.')]
class LeadController
{
    #[Get('', name: 'index')]
    #[WithPermission(PermissionEnum::VIEW_LEADS)]
    public function index(LeadListQuery $query): AnonymousResourceCollection { /* ... */ }

    #[Post('', name: 'store')]
    #[WithPermission(PermissionEnum::CREATE_LEAD)]
    #[Throttle(6)]
    public function store(LeadRequest $request): JsonResponse { /* ... */ }

    #[Delete('{lead}', name: 'destroy', where: ['lead' => '[0-9]+'])]
    #[WithPermission(PermissionEnum::DELETE_LEAD)]
    public function destroy(Lead $lead): JsonResponse { /* ... */ }
}

use AhmedNour\AttributeRouting\Attributes\{Get, Post, Prefix, Middleware, Name};

#[Get(
    path: 'reports/{report}',
    middleware: ['signed'],           // string or array
    prefix: 'admin',                  // extra prefix for this route only
    name: 'reports.show',
    where: ['report' => '[0-9]+'],
    withoutMiddleware: ['auth:sanctum'],
)]

#[Get('export', name: 'leads.export')]
#[Get('download')]
public function export(): StreamedResponse { /* ... */ }

#[Prefix('api/auth')]
#[Middleware(['api', 'auth:sanctum'])]
class LoginController
{
    // The one route in this controller that must stay public.
    #[Post('login', name: 'auth.login')]
    #[WithoutMiddleware('auth:sanctum')]
    #[Throttle(5)]
    public function login(LoginRequest $request): JsonResponse { /* ... */ }
}

#[Name('leads.')]                       // class
#[Get('', name: 'index')]               // → leads.index
#[Get('archive')]                       // → unnamed

use AhmedNour\AttributeRouting\Contracts\Permitted;

enum PermissionEnum: string implements Permitted
{
    case EDIT_TASK = 'edit_task';

    public function getMiddleware(): string
    {
        return 'permission:'.$this->value;
    }
}

#[WithPermission(PermissionEnum::EDIT_TASK)]
#[Put('{task}', name: 'tasks.update')]
public function update(Task $task): JsonResponse { /* ... */ }

#[WithPermission(Permission::EDIT_TASK)]
#[WithPermission(Permission::MANAGE_PROJECT)]
#[Put('{task}', name: 'tasks.update')]
public function update(Task $task): JsonResponse { /* ... */ }

return [
    // Turn discovery off without uninstalling. Ignored when routes are cached.
    'enabled' => env('ATTRIBUTE_ROUTING_ENABLED', true),

    // PSR-4 namespace prefix => directory to scan.
    'paths' => [
        'App\\' => app_path(),
    ],

    // Middleware format for permissions that aren't Permitted enums.
    'permission_middleware' => 'permission:%s',
];

'paths' => [
    'App\\Http\\Controllers\\' => app_path('Http/Controllers'),
    'App\\Modules\\' => app_path('Modules'),
],
bash
php artisan vendor:publish --tag=attribute-routing-config
bash
php artisan attribute-routing:list
php artisan attribute-routing:list --path=leads