The essential function of any web application framework is to take requests from a user and deliver the responses. Defining an application’s routes is the first and most important project to tackle.
Route Definitions
In a Laravel application, we can define ‘web’ routes in routes/web.php and API routes in routes/api.php
1 | Route::get('/',function () { |
Now, when you visit the ‘/‘ (route of your domain), you will see ‘Hello World’.
You can also define a route combined with some templates
1 | Route::get('/', function() { |
Route Verbs
In above examples, we are using Route::get which means we are telling Laravel to only match for the routes when HTTP request uses the GET action; however, normally, HTTP actions may inlclude GET, POST, PUT, DELETE and so on. We have a few other options to call on a route.
1 | Route::post('/', function() {}); |
Route Handling
Passing a closure to the route definition is not the only way to tell Laravel how to resolve a route. Additionally, applications using route closures can’t take advantage of Laravel’s route caching, which can shave up to hunders of milliseconds off of each request.
The common option is to pass a controller name and method as a string in place of the closure.
1 | Route::get('/','WelcomeController@index'); |
Above code means when Laravel receives the GET request, it will pass the request to WelcomeController (App\Controller\WelcomeController) and invoke the index() method
Route Parameters
If the route you’re defining has parameters, you can pass it like this
1 | Route::get('user/{id}', function($id) {}); |
You may ask, do I need to use the same names for both route parameter({id}) and the method parameter($id)?
Well, the answer is unless you are using route/model binding, no. Their order (left to right) defines which route parameter matches which method parameter. For example:
1 | Route::get('users/{userId}/comments/{commentId}', function ( $thisIsActuallyTheUserId, $thisisReallyTheCommentId ) { |
You can also make your route parameter optional by including a question mark (?)
1 | Route::get('user/{id?}', |
Note: Routes are matched from top to bottom. Let’s say we have two routes
1 | 1. Route::get('user/{$id}',function($id){ |
Now when you visit the ‘/user/verified’, it will return an error. This is becaue Laravel will match the first route and treat ‘verified’ as a paramater, then it will find a user by the id ‘verified’.
The solution is to move the second route to the top
1 | Route::get('user/verified', function(){ |
Route Names
The simplest way to refer to routes elsewhere in your application is by their path. There’s a url() helper to prefix your route with the full domain of your site.
1 | <a href="{{url('/')}}"> // we are using blade template here |
However, in Laravel we can name each route, which enables you to refer to it by using its name.
1 | // Define a route with name |
In Laravel 5.4 you can define the name like this
1 | Route::name('user.show')->get('user/{id}',function($id) {}); |
Route Groups
Route groups allow you to group several routes that sharing any configurations together.
Sharing same middleware
1 | Route::group(['middleware' => 'auth'], function () { |
Sharing same path prefixes
1 | Route::gropu(['prefix' => 'api'],function(){ |
Subdomain Routing
1 | Route::group(['domain' => 'api.yoursite.com'], function(){ |
Namespace Prefixes
1 | Route::group(['namespace'=>'API'],function() { |
Name Prefixes
1 | Route::group(['as' => 'users.'],function(){ |
Views
In Laravel, there are two formats of view you can use out of the box: plain PHP, or Blade templates. about.php will be render with the PHP engine, and about.blade.php will be rendered with Blade engine.
1 | Route::get('/', function(){ |
This code will looks after a view in resources/views/home.blade.php or resources/views/home.php and parses any inline PHP or control structures and eventually return the result to user.
You can pass in variables by this
1 | Route::get('/',function(){ |
It will find resources/views/tasks/index.blade.php or resources/views/tasks/index.php and passes a single variable named task
Controllers
Controllers are essentially classes that organize the logic of one or more routes together in on place. Think of controllers as the traffice cops that route HTTP requests around your application.
One easy way to create a controller is with an Artisan command
1 | php artisan make:controller TasksController |
Now you could find the controller in app/Http/Controllers
Get User Input
1 | // TasksController.php ... |
Resources controller
there’s a trick for generating routes, and it’s called “resource controller binding.”
1 | Route::resource('user','UserController'); |
And the command for create resource controller is
1 | php artisan make:controller UserController --resource |
Route Model Binding
Laravel provides a feature that simplifies this pattern called “route model binding.” This allows you to define that a particular parameter name (e.g., {conference}) will indicate to the route resolver that it should look up an Eloquent record with that ID and then pass it in as the parameter instead of just passing the ID.
There are two kinds of route model binding: implicit and explicit
Implicit
1
2
3Route::get('user/{user}', function(User $user){
return $user
});Becasue the parameter ({user}) is the same as the method parameter ($user), and the method parameter is typehinted with a User model (User $user), Laravel sees this as a route model binding. Every time this route is visited, the application will assume that whatever is passed into the URL in place of {user} is an ID that should be used to look up a User and then that resulting model instance will be passed in to your closure or controller method.
Note: By default, Eloquent will look it up by its primary key (ID), to change the column your Eloquent model uses for URL lookups, add a method to your model named getRouteKeyName():
1
2
3public function getRouteKeyName(){
return 'slug';
}Now, a URL like user/{user} will expect to get the slug instead of the ID, and will perform its lookups accordingly.
Explicit
To manually configure route model bindings, add a link to the boot() method in App\Providers\RouteServiceProvider.
1
2
3
4
5
6
7public function boot()
{
parent::boot();
// use Illuminate\Support\Facades\Route
Route::model('users', App\User::class);
}Now whenever a route has a parameter in its definition named {users}, as demonstrated in Example 3-30, the route resolver will return an instance of the Conference class with the ID of that URL parameter.
1
2
3
4Route::get('user/{users}', function(User $user){
// now the request parameter name could be different from //function parameter name (users vs $user)
return $user
});
Redirects
There are two common ways to generate a redirect: redirect global helper or Facade.
1 | Route::get('redirect-with-helper', function(){ |
redirect()->to()
the to() method looks like this:
1 | function to($to = null, $status = 302, $headers = [], $secure = null) |
redirect()->route()
The route() method is the same as the to() method, but rather than pointing to a particular path, it points to a particular route name
1 | Route::get('redirect',function(){ |
The route() function looks like this:
1 | function route($to = null, $parameters = [], $status = 302, $headers = []) |
So we can pass parameters to route by
1 | Route::get('redirect/{user}',function(User $user){ |
redirect()->back()
which simply redirects the user to whatever page she came from. There’s also a global shortcut for this: back().
redirect()->with()
1 | Route::get('redirect-shortcut-with-array',function(){ |