All Projects → spatie → Laravel Route Attributes

spatie / Laravel Route Attributes

Licence: mit
Use PHP 8 attributes to register routes in a Laravel app

Labels

Projects that are alternatives of or similar to Laravel Route Attributes

Phphub5
PHPHub Ver 5 is a Forum project Powered by Laravel 5.1, and it is also the project build up PHP & Laravel China community (此项目已弃用)
Stars: ✭ 1,971 (+1052.63%)
Mutual labels:  laravel
Laravel Translatable
[Deprecated] A Laravel package for multilingual models
Stars: ✭ 1,974 (+1054.39%)
Mutual labels:  laravel
Koel
🐦 A personal music streaming server that works.
Stars: ✭ 13,105 (+7563.74%)
Mutual labels:  laravel
Imall
基于Laravel5.2,Vue.js1.0的微信商城,用于熟悉 Laravel、Vuejs、Webpack、Gulp 的结合使用,已不维护及更新。(1MB单核基础服务器,浏览请耐心等待图片加载...)
Stars: ✭ 168 (-1.75%)
Mutual labels:  laravel
Lara Head
Easily setup SEO in your laravel project with lara-head ❤️ @code4mk
Stars: ✭ 169 (-1.17%)
Mutual labels:  laravel
Laravel Selfupdater
This package provides some basic methods to implement a self updating functionality for your Laravel application. Already bundled are some methods to provide a self-update mechanism via Github or some private repository via http.
Stars: ✭ 170 (-0.58%)
Mutual labels:  laravel
Themes
🎨 Laravel Themes package with support for the Caffeinated Modules package.
Stars: ✭ 167 (-2.34%)
Mutual labels:  laravel
Lapse
Laravel Self Hosted Tiny Error Tracking System With Notifications
Stars: ✭ 172 (+0.58%)
Mutual labels:  laravel
Laravel Bridge
Package to use Laravel on AWS Lambda with Bref
Stars: ✭ 168 (-1.75%)
Mutual labels:  laravel
Laravel Debugbar
Laravel Debugbar (Integrates PHP Debug Bar)
Stars: ✭ 13,485 (+7785.96%)
Mutual labels:  laravel
Laravel Google Captcha
Google captcha for Laravel 5, Laravel 6 and Laravel 7, support multiple captcha on page
Stars: ✭ 168 (-1.75%)
Mutual labels:  laravel
Fcm
Firebase Cloud Messaging (FCM) notifications channel for Laravel
Stars: ✭ 169 (-1.17%)
Mutual labels:  laravel
Laravel Movies Example
Code for YouTube series on building a Laravel Movie Application
Stars: ✭ 171 (+0%)
Mutual labels:  laravel
Partyline
Output to Laravel's console from outside of your Command classes.
Stars: ✭ 168 (-1.75%)
Mutual labels:  laravel
Multi Domain Laravel
An example of multi-domain/subdomain app in Laravel.
Stars: ✭ 171 (+0%)
Mutual labels:  laravel
Logviewer
📃 Provides a log viewer for Laravel
Stars: ✭ 2,098 (+1126.9%)
Mutual labels:  laravel
Laravel Nova Nested Form
This package allows you to include your nested relationships' forms into a parent form.
Stars: ✭ 169 (-1.17%)
Mutual labels:  laravel
Dv Php Core
Devless is a ready-made back-end for development of web or mobile applications. It is fully open source under the permissive Apache v2 license. This means that you can develop your front end without worrying about neither back-end code or the business risk of a proprietary backend-as-a-service.
Stars: ✭ 171 (+0%)
Mutual labels:  laravel
Laradminator
Integration of Adminator into Laravel 6.x/7.x/8.x with RTL support
Stars: ✭ 170 (-0.58%)
Mutual labels:  laravel
Novapackages
Stars: ✭ 169 (-1.17%)
Mutual labels:  laravel

Use PHP 8 attributes to register routes in a Laravel app

Latest Version on Packagist Tests Type Coverage

This 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 us

We 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.

Installation

You 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'),
    ],
];

Usage

The 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 route

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']);

Using other HTTP verbs

We 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')]

Using multiple verbs

To register a route for all verbs, you can use the Any attribute:

#[Spatie\RouteAttributes\Attributes\Any('my-uri')]

To register a route for a few verbs at once, you can use the Route attribute directly:

#[Spatie\RouteAttributes\Attributes\Route(['put', 'patch'], 'my-uri')]

Specify a route name

All HTTP verb attributes accept a parameter named name that accepts a route name.

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 middleware

All HTTP verb attributes accept a parameter named middleware that accepts a middleware class or an array of middleware classes.

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 Middleware attribute. You can mix this with applying attribute on a method.

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 prefix

You can use the Prefix annotation on a class to prefix the routes of all methods of that class.

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 domain

You can use the Domain annotation on a class to prefix the routes of all methods of that class.

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');

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].