What Is Routing And How, And What Are The Different Ways To Write It?

Routing Laravel

All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by the framework. The routes/web.php file defines routes that are for your web interface. These routes are assigned the web middleware group, which provides features like session state and CSRF protection. The routes in routes/api.php are stateless and are assigned the API middleware group. For most applications, you will begin by defining routes in your routes/web.php file.

Route::get('foo', function(){});
Route::get('foo', 'ControllerName@function');
Route::controller('foo', 'FooController');

RESTful Controllers
//Specify a subset of actions to handle on the route
 Route::resource('photo', 'PhotoController',['only' => ['index', 'show']]);
Route::resource('photo', 'PhotoController',['except' => ['update', 'destroy']]);

Triggering Errors
throw new NotFoundHttpException;

Route Parameters
Route::get('foo/{bar}', function($bar){});
Route::get('foo/{bar?}', function($bar = 'bar'){});

HTTP Verbs
Route::any('foo', function(){});
Route::post('foo', function(){});
Route::put('foo', function(){});
Route::patch('foo', function(){});
Route::delete('foo', function(){});
// RESTful actions
 Route::resource('foo', 'FooController');

Secure Routes
Route::get('foo', array('https', function(){}));
Route 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]'))

// Set a pattern to be used across routes
 Route::pattern('bar', '[0-9]+')

// 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'));

Named Routes
Route::get('foo/bar', array('as' => 'foobar', function(){}));

Route Prefixing
// This route group will carry the prefix 'foo'
 Route::group(array('prefix' => 'foo'), function(){})

Route Namespacing
// This route group will carry the namespace 'Foo\Bar'
 Route::group(array('namespace' => 'Foo\Bar'), function(){})

Sub-Domain Routing
// {sub} will be passed to the closure
 Route::group(array('domain' => '{sub}.example.com'), function(){});        

You may also like...

2 Responses

  1. I enjoy what you guys are up too. This sort of clever work and coverage!
    Keep up the excellent works guys I’ve added you guys to my own blogroll.

Leave a Reply

Your email address will not be published. Required fields are marked *

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codeforbetter will use the information you provide on this form to be in touch with you and to provide updates and marketing.