Download the PHP package sebastiansulinski/laravel-routes without Composer
On this page you can find all versions of the php package sebastiansulinski/laravel-routes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sebastiansulinski/laravel-routes
More information about sebastiansulinski/laravel-routes
Files in sebastiansulinski/laravel-routes
Package laravel-routes
Short Description Route Collections and Route Model Binding wrappers for Laravel
License MIT
Informations about the package laravel-routes
Route Collections and Route Model Binding wrappers for Laravel
[DEPRECATED] This repository is no longer maintained
While this project is functional with older versions of Laravel, the dependencies are no longer up to date. You are still welcome to explore, learn, and use the code provided here.
Route collections
Crate a new directory that will store all route collections for your application - example would be directory called Routes/Collections
under app/Http
.
Inside the directory create a new class that will be used with the given section of your system - say for the Front end of your application, we use FrontCollection.php
and we put it into another directory - to distinguish between sections - because this one belongs to the front end, we call it Front
.
The new FrontCollection
class will extend the SSD\LaravelRoutes\RouteCollectionFactory
and needs to have the method getNameSpace
that returns the current namespace, which will be used to create a fully qualifying name of each class within the app/Http/Routes/Collections/Front
directory.
Inside the same directory create a new file for each collection of routes - say for the Blog
module of your Front section you could use:
and perhaps another one for the Contact
Controller with form submission method:
Now all you need to do in your app/Http/routes.php
file is:
The magically called static methods on the FrontCollection
are names of the collection classes in camelCase
- say for instance collection with name FoodRecepies
would be called as FrontCollection::foodRecepies()
and so on.
If you want to keep your routes.php
file even cleaner, you could create a master collection for each section and then enclose all separate route collections inside of it
and for the Admin
section (make sure you first create AdminCollection
)
Then simply call it from within the routes.php
Custom exceptions
The abstract SSD\LaravelRoutes\RouteCollectionFactory
class can throw either SSD\LaravelRoutes\Exceptions\InvalidClassName
when the static method name does not correspond to the existing class or SSD\LaravelRoutes\Exceptions\MissingNamespace
when you forget to declare the getNameSpace()
method on the class extending SSD\LaravelRoutes\RouteCollectionFactory
.
Route model binder
Route model binder allows you to group model bindings.
To start, create a new directory under app/Http/Routes
called ModelBindings
Inside this directory create a class corresponding to the model you are trying to define bindings for - for instance, if you had a Blog
model on which you'd like to define two bindings - one for blog_id
and the other for the blog_slug
your model would look like so
Add the scopeWhereSlug()
method to your Blog
model (or, if you're using slugs on more than one model you could extract it to a Trait)
Now, with the BlogBinder
ready, we can add it to the `app/Providers/RouteServiceProvider.php'
Model binding tip
You are more likely to use slug
with the front end of your application - so just to boost the performance a bit, let's cache the model for the slug
binding.
To do this - create a new, BaseBinder
class under App\Http\Routes\ModelBindings
and to make it easier - let's use nespot/carbon
package. First add Carbon
dependency with the composer.
Now create BaseBinder
class with the cache
method. I'm caching for just one day, but feel free to make it as long as you need
Finally modify the BlogBinder
class
And now your model binding will first be served from the database, then, every sub-sequent call will be read from cache for a length of one day. Make sure that when you update record - you also update cached version - or simply remove cache for a corresponding key.