Skip to main content

Routing

laravel-framework

  • public/index.php : all request will go through this entry point
  • routes/web.php : route will redirect to specific controller according to the url
  • views/?.blade.php: template controllers responsible for generating response

Set Routing

  • go to routes/web.php
Route::get('/',function(){
return view('welcome');
});

Route::view('/','welcome'); //can be simplified with Route::view

Route::get('/contact',function(){
return view('home.contact'); //home folder, contact.blade.php file
})->name('home.contact'); //giving name to route


Route::get('/posts/{id}',function($id){ //route with required parameters
return view('posts',['id'=>$id]);
});

Route::get('/posts/{id?}',function($id=1){ //route with optional parameter with default value
return view('posts',['id'=>$id]);
});

$posts = [1=>['title'=>'HTML','content'=>'intro to HTML'],2=>['title'=>'PHP','content'=>'intro to PHP']];

Route::get('/posts/{id}',function($id) use($posts){
abort_if(!isset($posts[$id]),404); //if no object is found, return 404 error
return view('posts',[ 'post'=> $posts[$id] ]); //pass an object
});

show routing table

in terminal, type the following command

php artisan route:list

route-list