Skip to content

Instantly share code, notes, and snippets.

@shahednur
Created February 9, 2019 04:18
Show Gist options
  • Save shahednur/6c98561b8e47cda7f18d09e6cd00606a to your computer and use it in GitHub Desktop.
Save shahednur/6c98561b8e47cda7f18d09e6cd00606a to your computer and use it in GitHub Desktop.
Laravel Route Group Collections
=========================
======Basic Route========
=========================
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::match(['get', 'post'], '/',function (){});
Route::any('foo', function (){});
Route::redirect('/here','/there', 301);
Route::permanentRedirect('/here', '/there');
Route::view('/welcome', 'welcome', ['name' => 'Taylor']);
Route::pattern('id', '[0-9]+');
================================================
======register multiple HTTP verbs Route========
================================================
Route::match(['get', 'post'], '/', function(){return 'Hello World';});
========================================
======Route Parameters(Required)========
========================================
Route::get('user/{id}', function ($id){return 'User Id is:'.$id;});
========================================
======Route Parameters(Optional)========
========================================
Route::get('customer/{name?}', function ($name = null) {return $name;});
==========================
======Named Routes========
==========================
Route::get('/task/{id}', function($id){return route('task',['id' => 2]);})->where('id', '[0-9]+')->name('task');
==========================
====== Route Groups=======
==========================
Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function(){
Route::get('dashboard', function() {} );
Route::get('settings', function() {} );
});
================================
======Route Model Binding=======
================================
Route::get('posts/{post}', function ($post){return view('post.show', compact('post'));});
=======================
======Middleware=======
=======================
Route::middleware(['first', 'second'])->group(function () {
Route::get('/', function () {// Uses first & second Middleware});
Route::get('user/profile', function () {// Uses first & second Middleware});
});
=======================
======Namespaces=======
=======================
Route::namespace('Admin')->group(function () {// Controllers Within The "App\Http\Controllers\Admin" Namespace});
===============================
======Sub-Domain Routing=======
===============================
Route::domain('{account}.myapp.com')->group(function () {
Route::get('user/{id}', function ($account, $id) {//});
});
===========================
======Route Prefixes=======
===========================
Route::prefix('admin')->group(function () {Route::get('users', function () {// Matches The "/admin/users" URL});});
================================
======Route Name Prefixes=======
================================
Route::name('admin.')->group(function () {
Route::get('users', function () {// Route assigned name "admin.users"...})->name('users');
});
#Namespace Prefixes:Problem
namespace App\Http\Controllers\Admin;
class NewsController extends Controller{}
Route::get('admin/news', ['uses' => 'Admin\NewsController@index']);
solution:
Route::group(['namespace' => 'Admin'], function(){
Route::get('admin/news', ['uses' => 'NewsController@index']);
Route::get('admin/users', ['uses' => 'UserController@index']);
});
#Name Prefixes:Problem
#http://fakebook.dev/admin/news
#http://fakebook.dev/admin/users
solution:
Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function(){
Route::get('news', ['uses' => 'NewsController@index']);
Route::get('users', ['uses' => 'UserController@index']);
});
#Displaying User Status:
Route::group(['domain' => 'fakebook.dev'], function(){
Route::any('/', function(){ return 'My own domain';});
});
Route::group(['domain' => '{username}.fakebook.dev'], function(){
Route::any('/', function($username){return 'You visit your account: '. $username;});
$data_user = [
'masud' => ['profile' => ' a cute programmer. ', 'status' => [ 'I\'m cool!', 'I\'m cool very Cool!', 'Fantastic!'] ],
'sohel' => [ 'profile' => 'a boss programmer.' ,'status' => [ 'Sweet!', 'Today is incredible!', 'Nice ..']]
];
Route :: get ('status/{id}', function ($username, $id) use ($data_user){
return $username. ' writes: '. $data_user [$username] ['status'] [$id];
});
});
#Display User Profile with Laravel Subdomain and Group Routing Feature:
Route::group(['domain' => 'fakebook.dev'], function(){
Route::any('/', function(){return 'My own domain';});
});
Route::group(['domain' => '{username}.fakebook.dev'], function(){
Route::any('/', function($username){return 'You visit your account: '. $username;});
$data_user = [
'masud' => [
'profile' => ' a cute programmer. ',
'status' => [ 'I\'m cool!', 'I\'m cool very Cool!','Fantastic!']
],
'sohel' => [
'profile' => 'a boss programmer.' ,
'status' => [ 'Sweet!', 'Today is incredible!', 'Nice ..']
]
];
Route :: get ( 'profile', function ($username) use ($data_user){
return $username." is a ".$data_user[$username] [ 'profile'];
});
});
#Add Subdomain Routing Code:
Route::group(['domain' => 'fakebook.dev'], function(){
Route::any('/', function(){return 'My own domain';});
});
Route::group(['domain' => '{username}.fakebook.dev'], function(){
Route::any('/', function($username){return 'You visit your account: '. $username;});
});
#Account:
Route::group(['prefix' => 'account', 'as' => 'account.'], function () {
Route::get('login', ['as' => 'login', 'uses' => 'AccountController@getLogin']);
Route::get('register', ['as' => 'register', 'uses' => 'AccountController@getRegister']);
Route::group(['middleware' => 'auth'], function () {
Route::get('edit', ['as' => 'edit', 'uses' => 'AccountController@getEdit']);
});
});
#Results:
<a href="{{ route('account.login') }}">Login</a>
<a href="{{ route('account.register') }}">Register</a>
<a href="{{ route('account.edit') }}">Edit Account</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment