Laravel 5: HTTP Layer – Routes

Laravel 5: HTTP Layer – Routes


Routes, in general, are like a switchboard.
Request comes in and routing system directs it into a proper resource, that is equipped to handle it.

A small digression.

As you noticed, I wrote ‘in general’ above.
It is because – in my opinion – routes should be lean.
They should do traffic management with some datatype filtering, middleware and no more.

Laravel offers more, which … again, in my opinion, clouds routes mission.

I am talking about model binding.

It may come handy, but I prefer to deal with objects and object injection elsewhere allowing routes to concentrate doing, what they were created to do: play traffic cops.

Of course unless, you have to do a little peak inside of a model data to aid filtering at this early level, or better traffic management.
But that’s about it.

 

Laravel uses most of HTTP verbs in its routing of incoming request to a resource that can deal with request.

Here they are: getpostputpatchdeleteoptions.

Lets see what these methods do.

  • get
    read (but also can be used for other actions, like: post, put/patch and even delete)
  • post
    create/insert
  • put/patch
    update/replace/modify
  • delete
    delete
  • options
    The options method returns the http methods that the server supports for the specified url. This can be used to check the functionality of a web server by requesting ‘*’ instead of a specific resource. You would not be using this too often.

Mostly we use get and post, unless we go into RESTful API. You will definitely be using them, if you decide to go for Resource Controllers.

 

Route Construction

Laravel routes have pretty much two ways they get built:

callback

… where all action takes place in callback.

controller action

… where request is redirected to specific controller’s action – in this case: showProfile.

 

Capturing Multiple Routes
route matching multiple verbs/metods

route matching ALL verbs

note:

Use CSRF Protection for post, put/patch, delete – anything, that causes change and uses form must be protected.

 

Parameters Captured By Route And Passed To Clousure To Be Used Inside
Some general pointers:
  1. set as many arguments in GET as needed,
  2. each parameter is captured by variable in callback function
  3. add (?) if you expect sending/receiving no value, but in such case you must set default in route – you can use NULL

Route parameters are always encased within {} braces and should consist of alphabetic characters.

Route parameters may not contain a (-) character. Use an underscore (_) instead.

 

Use Routes To Validate Incoming Values
in route

global – in boot method of RouteServiceProvider

Once the pattern has been defined, it is automatically applied to all routes using that parameter name – in this case ‘id’:

No need to use chained ‘where’ condition in route anymore.

 

Naming/Defining Routes So They Can Be Used Anywhere

 

You can define route, name it and use it later

or …

also like this:

Now, you have route named ‘profile‘ defined.

Using named route

You can pass params to named route

 

Protecting Routes By Middleware

Middleware is a way to do something to content overseen by them.

You can authenticate, filter etc. many content by putting access routes to them inside of given middleware.

Middleware is like a layers of protection request has to go through, before it can reach its destination.

 

Grouping Routes: Prefix And Namespace

First, let’s see our sample route.

routes_flat

Prefix

Red part allows you to group your routes by ‘prefix’.
If you have a lot of addresses with ‘prefix’ users, like for instance:

  • user/admin
  • user/client
  • user/log

etc… you can group them by such prefix like this:

Namespace

Blue part allows tyou to group your routes by ‘namespace’.
Lets say, you have separate folder /User inside of /Http/Controllers.

We can access them seprately, like this:

… or we can group them by namespace, like this:

Combining namespace and prefix

To organize things (especially important in complex app with many routes) and cut down on typing, we can combine namespace and prefix:

Now, we no longer need to type all URI prefixes and namespaces.


More – read here or here.