Routing
Available Router Methods
<?php
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
Route::any($uri, $callback); //Response to any type of request
Make a route secure
Route::get($uri , array('https', $callback ));
Route Naming
Put a name for a route
Route::<REQUEST_METHOD>($uri, ['as' => 'ROUTE_NAME' , $callback]);
OR
Route::<REQUEST_METHOD>($uri, $callback )->name( 'ROUTE_NAME' );
REQUEST_METHOD:
get , post , put , delete , [ so on .... ]
Get current Route name
$route = Route::currentRouteName();
Generating URLs To Named Routes
// Generating URLs...
$url = route( $routeName );
// Generating Redirects...
return redirect()->route( $routeName );
Route Parameter
Route::get('user/{id}', function ($id) {
return 'User '.$id;
});
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
//
});
Optional parameters
Route::get('user/{name?}', function ($name = null) { // default null to $name
return $name;
});
Route::get('user/{name?}', function ($name = 'Rayhan') { // default 'Rayhan' to $name
return $name;
});
For using optionsal parameter, you need to specify a default value of your parameters
Parameter naming Constraints
Route::get('foo/{bar}', function($bar){})
->where('bar', '[0-9]+');
Route::get('foo/{bar}/{baz}', function($bar, $baz){})
->where(array('bar' => '[0-9]+', 'baz' => '[A-Za-z]'))
Route Prefixing
Route::group( ['prefix' => $prefixUri ] , $subRoutes);
OR
Route::prefix( $prefixUri )->group( $subRoutes);
Example
// ** Route Links **
//-> admin/posts
//-> admin/pages
//-> admin/categories
Route::group(['prefix' => 'admin'] , function(){
Route::get('/posts' , function(){
//
});
Route::get('/pages' , function(){
//
});
Route::get('/categories' , function(){
//
});
});
Controller Route
Call a specific method of a controller
Route::<Route_METHOD>( $uri , 'ControllerName@method');
RESTful resource route
Route::resource( $uri , $controllerName );
Make only work some specific method of a controller
Route::resource('photo', 'PhotoController', ['only' => [
'index', 'show'
]]);
Exclue some methods from of a controller
Route::resource('photo', 'PhotoController', ['except' => [
'create', 'store', 'update', 'destroy'
]]);
Filters
// Declare an auth filter
Route::filter('auth', function(){});
// Register a class as a filter
Route::filter('foo', 'FooFilter');
Route::get('foo', array('before' => 'auth', function(){}));
// Routes in this group are guarded by the 'auth' filter
Route::get('foo', array('before' => 'auth', function(){}));
Route::group(array('before' => 'auth'), function(){});
// Pattern filter
Route::when('foo/*', 'foo');
// HTTP verb pattern
Route::when('foo/*', 'foo', array('post'));
view Route
If your route only needs to return a view, you may use the Route::view
method.
Route::view($uri , $viewName);
Example:
// route for contact.blade.php
Route::view('/contact' , 'contact');
^^^ ^^^
$uri $viewName
Accessing The Current Route
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();