PHP code example of spatie / laravel-route-attributes
1. Go to this page and download the library: Download spatie/laravel-route-attributes 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/ */
spatie / laravel-route-attributes example snippets
use Spatie\RouteAttributes\Attributes\Get;
class MyController
{
#[Get('my-route')]
public function myMethod()
{
}
}
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.
*
* 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'
],
],
];
'directories' => [
base_path('app-modules/Blog') => [
// only register routes in files that match the patterns
'patterns' => ['*Controller.php'],
// do not register routes in files that match the patterns
'not_patterns' => ['*Test.php'],
],
],
use Spatie\RouteAttributes\Attributes\Get;
class MyController
{
#[Get('my-route')]
public function myMethod()
{
}
}
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()
{
}
}
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()
{
}
}
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()
{
}
}
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()
{
}
}
use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\ScopeBindings;
class MyController
{
#[Get('users/{user}/posts/{post}')]
#[ScopeBindings]
public function getUserPost(User $user, Post $post)
{
return $post;
}
}
Route::get('/users/{user}/posts/{post}', function (User $user, Post $post) {
return $post;
})->scopeBindings();
#[ScopeBindings(false)]
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()
{
}
}
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()
{
}
}
use Spatie\RouteAttributes\Attributes\Defaults;
use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\Post;
#[Defaults('param', 'controller-default')]
class MyController extends Controller
{
#[Get('my-get-route/{param?}')]
public function myGetMethod($param)
{
}
#[Post('my-post-route/{param?}/{param2?}')]
#[Defaults('param2', 'method-default')]
public function myPostMethod($param, $param2)
{
}
#[Get('my-default-route/{param?}/{param2?}/{param3?}')]
#[Defaults('param2', 'method-default-first')]
#[Defaults('param3', 'method-default-second')]
public function myDefaultMethod($param, $param2, $param3)
{
}
#[Get('my-override-route/{param?}')]
#[Defaults('param', 'method-default')]
public function myOverrideMethod($param)
{
}
}
use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\Post;
use Spatie\RouteAttributes\Attributes\WithTrashed;
#[WithTrashed]
class MyController extends Controller
{
#[Get('my-get-route/{param}')]
#[WithTrashed]
public function myGetMethod($param)
{
}
#[Post('my-post-route/{param}')]
#[WithTrashed(false)]
public function myPostMethod($param)
{
}
#[Get('my-default-route/{param}')]
public function myDefaultMethod($param)
{
}
}