在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):spatie/laravel-route-attributes开源软件地址(OpenSource Url):https://github.com/spatie/laravel-route-attributes开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):Use PHP 8 attributes to register routes in a Laravel appThis package provides annotations to automatically register routes. Here's a quick example: use Spatie\RouteAttributes\Attributes\Get;
class MyController
{
#[Get('my-route')]
public function myMethod()
{
}
} This attribute will automatically register this route: Route::get('my-route', [MyController::class, 'myMethod']); Are you a visual learner?In this video you'll get an introduction to PHP 8 attributes and how this laravel-routes-attributes works under the hood. Support usWe invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products. We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall. InstallationYou can install the package via composer: composer require spatie/laravel-route-attributes You can publish the config file with: php artisan vendor:publish --provider="Spatie\RouteAttributes\RouteAttributesServiceProvider" --tag="config" This is the contents of the published config file: return [
/*
* Automatic registration of routes will only happen if this setting is `true`
*/
'enabled' => true,
/*
* Controllers in these directories that have routing attributes
* will automatically be registered.
*/
'directories' => [
app_path('Http/Controllers'),
],
]; For controllers outside of the applications root namespace directories can also be added usin a 'directories' => [
'Modules\Admin\Http\Controllers\\' => base_path('admin-module/Http/Controllers'),
app_path('Http/Controllers'),
], UsageThe package provides several annotations that should be put on controller classes and methods. These annotations will be used to automatically register routes Adding a GET routeuse Spatie\RouteAttributes\Attributes\Get;
class MyController
{
#[Get('my-route')]
public function myMethod()
{
}
} This attribute will automatically register this route: Route::get('my-route', [MyController::class, 'myMethod']); Using other HTTP verbsWe have left no HTTP verb behind. You can use these attributes on controller methods. #[Spatie\RouteAttributes\Attributes\Post('my-uri')]
#[Spatie\RouteAttributes\Attributes\Put('my-uri')]
#[Spatie\RouteAttributes\Attributes\Patch('my-uri')]
#[Spatie\RouteAttributes\Attributes\Delete('my-uri')]
#[Spatie\RouteAttributes\Attributes\Options('my-uri')] Resource controllersTo register a resource controller, use the You can use You can use the Using use Spatie\RouteAttributes\Attributes\Resource;
#[Prefix('api/v1')]
#[Resource('posts', except: ['create', 'edit', 'destroy'], names: 'api.v1.posts')]
class PostController
{
public function index()
{
}
public function store(Request $request)
{
}
public function show($id)
{
}
public function update(Request $request, $id)
{
}
} Using multiple verbsTo register a route for all verbs, you can use the #[Spatie\RouteAttributes\Attributes\Any('my-uri')] To register a route for a few verbs at once, you can use the #[Spatie\RouteAttributes\Attributes\Route(['put', 'patch'], 'my-uri')] Specify a route nameAll HTTP verb attributes accept a parameter named use Spatie\RouteAttributes\Attributes\Get;
class MyController
{
#[Get('my-route', name: "my-route-name")]
public function myMethod()
{
}
} This attribute will automatically register this route: Route::get('my-route', [MyController::class, 'myMethod'])->name('my-route-name'); Adding middlewareAll HTTP verb attributes accept a parameter named use Spatie\RouteAttributes\Attributes\Get;
class MyController
{
#[Get('my-route', middleware: MyMiddleware::class)]
public function myMethod()
{
}
} This annotation will automatically register this route: Route::get('my-route', [MyController::class, 'myMethod'])->middleware(MyMiddleware::class); To apply middleware on all methods of a class you can use the use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\Middleware;
#[Middleware(MyMiddleware::class)]
class MyController
{
#[Get('my-route')]
public function firstMethod()
{
}
#[Get('my-other-route', middleware: MyOtherMiddleware::class)]
public function secondMethod()
{
}
} These annotations will automatically register these routes: Route::get('my-route', [MyController::class, 'firstMethod'])->middleware(MyMiddleware::class);
Route::get('my-other-route', [MyController::class, 'secondMethod'])->middleware([MyMiddleware::class, MyOtherMiddleware]); Specifying a prefixYou can use the use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\Post;
use Spatie\RouteAttributes\Attributes\Prefix;
#[Prefix('my-prefix')]
class MyController
{
#[Get('my-get-route')]
public function myGetMethod()
{
}
#[Post('my-post-route')]
public function myPostMethod()
{
}
} These annotations will automatically register these routes: Route::get('my-prefix/my-get-route', [MyController::class, 'myGetMethod']);
Route::post('my-prefix/my-post-route', [MyController::class, 'myPostMethod']); Specifying a domainYou can use the use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\Post;
use Spatie\RouteAttributes\Attributes\Domain;
#[Domain('my-subdomain.localhost')]
class MyController
{
#[Get('my-get-route')]
public function myGetMethod()
{
}
#[Post('my-post-route')]
public function myPostMethod()
{
}
} These annotations will automatically register these routes: Route::get('my-get-route', [MyController::class, 'myGetMethod'])->domain('my-subdomain.localhost');
Route::post('my-post-route', [MyController::class, 'myPostMethod'])->domain('my-subdomain.localhost'); Specifying a domain from a config keyThere maybe a need to define a domain from a configuration file, for example where your subdomain will be different on your development environment to your production environment.
use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\Post;
use Spatie\RouteAttributes\Attributes\DomainFromConfig;
#[DomainFromConfig('domains.main')]
class MyController
{
#[Get('my-get-route')]
public function myGetMethod()
{
}
} When this is parsed, it will get the value of Route::get('my-get-route', [MyController::class, 'myGetMethod'])->domain('example.com'); Specifying wheresYou can use the use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\Post;
use Spatie\RouteAttributes\Attributes\Where;
use Spatie\RouteAttributes\Attributes\WhereAlphaNumeric;
#[Where('my-where', '[0-9]+')]
class MyController
{
#[Get('my-get-route/{my-where}')]
public function myGetMethod()
{
}
#[Post('my-post-route/{my-where}/{my-alpha-numeric}')]
#[WhereAlphaNumeric('my-alpha-numeric')]
public function myPostMethod()
{
}
} These annotations will automatically register these routes: Route::get('my-get-route/{my-where}', [MyController::class, 'myGetMethod'])->where(['my-where' => '[0-9]+']);
Route::post('my-post-route/{my-where}/{my-alpha-numeric}', [MyController::class, 'myPostMethod'])->where(['my-where' => '[0-9]+', 'my-alpha-numeric' => '[a-zA-Z0-9]+']); For convenience, some commonly used regular expression patterns have helper attributes that allow you to quickly add pattern constraints to your routes. #[WhereAlpha('alpha')]
#[WhereAlphaNumeric('alpha-numeric')]
#[WhereNumber('number')]
#[WhereUuid('uuid')] Specifying a groupYou can use the use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\Post;
use Spatie\RouteAttributes\Attributes\Domain;
#[Group(domain: 'my-subdomain.localhost', prefix: 'my-prefix')]
#[Group(domain: 'my-second-subdomain.localhost', prefix: 'my-second-prefix')]
class MyController
{
#[Get('my-get-route')]
public function myGetMethod()
{
}
#[Post('my-post-route')]
public function myPostMethod()
{
}
} These annotations will automatically register these routes: Route::get('my-get-route', [MyController::class, 'myGetMethod'])->prefix('my-prefix')->domain('my-subdomain.localhost');
Route::post('my-post-route', [MyController::class, 'myPostMethod'])->prefix('my-prefix')->domain('my-subdomain.localhost');
Route::get('my-get-route', [MyController::class, 'myGetMethod'])->prefix('my-second-prefix')->domain('my-second-subdomain.localhost');
Route::post('my-post-route', [MyController::class, 'myPostMethod'])->prefix('my-second-prefix')->domain('my-second-subdomain.localhost'); Testingcomposer test ChangelogPlease see CHANGELOG for more information on what has changed recently. ContributingPlease see CONTRIBUTING for details. Security VulnerabilitiesPlease review our security policy on how to report security vulnerabilities. CreditsLicenseThe MIT License (MIT). Please see License File for more information. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论