- Notifications
You must be signed in to change notification settings - Fork13
THIS PACKAGE HAS BEEN DEPRECATED — An organized approach to handling routes in Laravel.
License
sebastiaanluca/laravel-router
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
An organized approach to handling routes in Laravel.
This package provides you with an easy-to-use system to separate route logic intorouters based on functionality while also providing additional functionality. A replacement for those bulkyweb.php andapi.php route files that are often lacking any structure and break Laravel structure conventions of separating everything in classes instead of regular PHP files.
Do note that itchanges nothing to the way you define your routes. It's just a way of organizing them. Optionally you can use the additional functionality it provides, but that's not a requirement.
- Requirements
- How to install
- How to use
- Optional features
- License
- Change log
- Testing
- Contributing
- Security
- Credits
- About
- PHP 7.3 or higher
- Laravel 7.0 or higher
Looking for support for earlier versions? Try out any of the previous package versions.
Just add the package to your project using Composer and Laravel will auto-discover it:
composer require sebastiaanluca/laravel-router
If you want to be able to register your routersin a single place, add theRegistersRouters trait to your HTTP kernel (found atApp\Http\Kernel):
<?phpnamespaceApp\Http;useIlluminate\Foundation\Http\KernelasHttpKernel;useSebastiaanLuca\Router\Kernel\RegistersRouters;class Kernelextends HttpKernel{use RegistersRouters;}
The following is an example of a router. It can be placed anywhere you like, though I'd suggest grouping them in theApp\Http\Routers directory.
<?phpnamespaceApp\Http\Routers;useSebastiaanLuca\Router\Routers\Router;class UserRouterextends Router{/** * Map the routes. */publicfunctionmap() {$this->router->group(['middleware' => ['web','guest']],function () {$this->router->get('/users',function () {returnview('users.index'); }); }); }}
Themap method is where you should define your routes and is theonly requirement when using a router. The Laravel routing instance is automatically resolved from the IoC container, so you can use any standard routing functionality you want. Of course you can also use theRoute facade.
To automatically have the framework load your router and map its routes,add the trait and add the router to the$routers array in your application's HTTP kernel class:
/** * The application routers to automatically boot. * * @var array */protected$routers = [ \App\Http\Routers\UserRouter::class,];
If you don't want to or can't add the trait to the kernel, you can also register the router manually by just instantiating it (in a service provider for instance). The parent base router will automatically resolve all dependencies and call themap method on your router.
app(\App\Http\Routers\UserRouter::class);
Especially useful in packages!
To use the following optional features, register theRegisterRoutePatterns class:
<?phpnamespaceApp\Http;useIlluminate\Foundation\Http\KernelasHttpKernel;useSebastiaanLuca\Router\Kernel\RegistersRouters;class Kernelextends HttpKernel{use RegistersRouters;/** * The application routers to automatically boot. * * @var array */protected$routers = [ \SebastiaanLuca\Router\Routers\RegisterRoutePatterns::class, ];}
Laravel provides a convenient way to validate URL parameters usingpatterns in routes. This package provides a predefined set of such patterns so you don't have to repeatedly add them to each route or define them yourself. The following parameter patterns are currently included:
- id (
\d+) - hash (
[a-z0-9]+) - uuid (
[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}) - slug (
[a-z0-9-]+) - token (
[a-zA-Z0-9]{64})
So forget about writing:
Route::get('user/activations/{uuid}',function ($uuid) {returnview('users.activations.show');})->where('uuid','[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}');
Just use the{uuid} or any other pattern in your route:
$this->router->get('user/activations/{uuid}',function ($uuid) {returnview('users.activations.show');});
Another great feature of Laravel issub-domain routing which allows you to handle multiple subdomains within a single Laravel project. The only caveat there is that it only does that and doesn't handle full domains.
Laravel Router fixes that for you so you can direct multiple domains to a single Laravel project and handle them all at once. Simply define a route group with the{domain} pattern and use it in your callback or controller:
$this->router->group(['domain' =>'{domain}'],function () {$this->router->get('user/{id}',function ($domain,$id) {return'You\'re visiting from' .$domain; });});
This package operates under the MIT License (MIT). Please seeLICENSE for more information.
Please seeCHANGELOG for more information what has changed recently.
composer installcomposertestPlease seeCONTRIBUTING andCONDUCT for details.
If you discover any security related issues, please emailhello@sebastiaanluca.com instead of using the issue tracker.
My name is Sebastiaan and I'm a freelance Laravel developer specializing in building custom Laravel applications. Check out myportfolio for more information,my blog for the latest tips and tricks, and my otherpackages to kick-start your next project.
Have a project that could use some guidance? Send me an e-mail athello@sebastiaanluca.com!
About
THIS PACKAGE HAS BEEN DEPRECATED — An organized approach to handling routes in Laravel.
Topics
Resources
License
Code of conduct
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.