Route
XinAdmin implements controller attribute-based routing, defining routes, middleware, and permissions by adding PHP Attribute annotations to controllers.
How It Works
AnnoRouteService scans all controller classes with #[RequestAttribute] annotation, parses class-level and method-level annotations, and automatically registers corresponding routes.
Core Components
Controller Class Annotation
Use #[RequestAttribute] on controller class:
<?php
namespace App\Http\Controllers\Admin;
use Xin\AnnoRoute\RequestAttribute;
#[RequestAttribute(
routePrefix: '/admin/user',
abilitiesPrefix: 'admin',
middleware: ['admin.auth'],
authGuard: 'admin'
)]
class UserController extends Controller
{
// Controller methods
}
Parameters:
Method Route Annotations
Use route annotations on controller methods to define individual routes:
GetRoute - GET Request
use Xin\AnnoRoute\Route\GetRoute;
#[GetRoute(
route: '/list',
authorize: 'user.list',
middleware: ['throttle:60,1'],
where: ['id' => '[0-9]+']
)]
public function list(): JsonResponse
{
// ...
}
PostRoute - POST Request
use Xin\AnnoRoute\Route\PostRoute;
#[PostRoute(
route: '/create',
authorize: 'user.create',
middleware: [],
where: []
)]
public function create(): JsonResponse
{
// ...
}
PutRoute - PUT Request
use Xin\AnnoRoute\Route\PutRoute;
#[PutRoute(
route: '/{id}',
authorize: 'user.update',
where: ['id' => '[0-9]+']
)]
public function update(int $id): JsonResponse
{
// ...
}
DeleteRoute - DELETE Request
use Xin\AnnoRoute\Route\DeleteRoute;
#[DeleteRoute(
route: '/{id}',
authorize: 'user.delete',
where: ['id' => '[0-9]+']
)]
public function delete(int $id): JsonResponse
{
// ...
}
Route Annotation Parameters:
CRUD Annotations
CRUD annotations are class-level annotations. When used, they automatically look for $service property in the controller as the repository class and generate corresponding route handler closures.
Define Repository Service
Controller must define $service property:
<?php
namespace App\Http\Controllers\Admin;
use App\Repository\UserRepository;
use Xin\AnnoRoute\RequestAttribute;
use Xin\AnnoRoute\Crud\Query;
use Xin\AnnoRoute\Crud\Find;
use Xin\AnnoRoute\Crud\Create;
use Xin\AnnoRoute\Crud\Update;
use Xin\AnnoRoute\Crud\Delete;
#[RequestAttribute(routePrefix: '/admin/user', abilitiesPrefix: 'admin')]
#[Query, Find, Create, Update, Delete]
class UserController extends Controller
{
protected string $service = UserRepository::class;
}
Query - Query List
#[Query(
middleware: ['throttle:60,1'],
where: []
)]
- HTTP Method: GET
- Route Address:
/
- Default Permission:
query
- Repository Method:
query($request->query())
Find - Find Single
#[Find(
middleware: [],
where: ['id' => '[0-9]+']
)]
- HTTP Method: GET
- Route Address:
/{id}
- Default Permission:
find
- Route Constraint:
id must be numeric
- Repository Method:
find($id)
Create - Create New
#[Create(
middleware: [],
where: []
)]
- HTTP Method: POST
- Route Address:
/
- Default Permission:
create
- Repository Method:
create($request->all())
Update - Update
#[Update(
middleware: [],
where: ['id' => '[0-9]+']
)]
- HTTP Method: PUT
- Route Address:
/{id}
- Default Permission:
update
- Route Constraint:
id must be numeric
- Repository Method:
update($id, $request->all())
Delete - Delete
#[Delete(
middleware: [],
where: ['id' => '[0-9]+']
)]
- HTTP Method: DELETE
- Route Address:
/{id}
- Default Permission:
delete
- Route Constraint:
id must be numeric
- Repository Method:
delete($id)
Complete Examples
Example 1: Using CRUD Annotations
<?php
namespace App\Http\Controllers\Admin;
use App\Repository\ArticleRepository;
use Xin\AnnoRoute\RequestAttribute;
use Xin\AnnoRoute\Crud\Query;
use Xin\AnnoRoute\Crud\Find;
use Xin\AnnoRoute\Crud\Create;
use Xin\AnnoRoute\Crud\Update;
use Xin\AnnoRoute\Crud\Delete;
#[RequestAttribute(
routePrefix: '/admin/article',
abilitiesPrefix: 'admin',
middleware: ['admin.auth']
)]
#[Query, Find, Create, Update, Delete]
class ArticleController extends Controller
{
public function __construct(
protected ArticleService $service
) {}
}
Generated Routes:
Generated Routes:
Example 2: Public Endpoints Without Permission Control
<?php
namespace App\Http\Controllers\Api;
use Xin\AnnoRoute\RequestAttribute;
use Xin\AnnoRoute\Route\GetRoute;
use Xin\AnnoRoute\Route\PostRoute;
#[RequestAttribute(routePrefix: '/api/public')]
class PublicController extends Controller
{
#[GetRoute(route: '/banner')]
public function banner(): JsonResponse
{
// Requires login...
}
#[PostRoute(route: '/feedback', authorize: false)]
public function feedback(): JsonResponse
{
// No permission verification...
}
}
authorize Parameter:
Route Annotation Common Properties
All route annotations inherit from BaseAttribute with the following properties:
Middleware Composition
Middleware can be set globally (class-level) and locally (method-level), and will be merged:
#[RequestAttribute(
routePrefix: '/admin/user',
middleware: ['admin.auth'] // Global middleware
)]
class UserController extends Controller
{
#[PostRoute(
route: '/create',
middleware: ['throttle:60,1'] // Additional local middleware
)]
public function create(): JsonResponse
{
// Final middleware: ['admin.auth', 'throttle:60,1']
}
}
Permission Ability Composition
Permission ability is composed of abilitiesPrefix and authorize:
#[RequestAttribute(
routePrefix: '/admin/user',
abilitiesPrefix: 'admin' // Abilities prefix
)]
#[Create]
class UserController extends Controller
{
// Create annotation's authorize defaults to 'create'
// Final permission ability: 'admin.create'
}