Skip to content

Instantly share code, notes, and snippets.

@moradi-morteza
Last active December 22, 2019 17:57
Show Gist options
  • Save moradi-morteza/5e771b24dfea5b94172c4a87871feaa0 to your computer and use it in GitHub Desktop.
Save moradi-morteza/5e771b24dfea5b94172c4a87871feaa0 to your computer and use it in GitHub Desktop.
[routing] #LaravelT
php artisan route:list // show all list of routes
//orginal ---------------------------------------
Route::get('/','HomeController@index');
//without method name ----------------------------
// if you not mention name of method so you should use __invoke():
Route::get('/{name}','HomeController');
class HomeController extends Controller{
public function __invoke($name){
return $name;
}
}
// if you do not want to give parameter every time you should convert it to optional :
Route::get('/{name?}','HomeController');
class HomeController extends Controller{
public function __invoke($name==null){
if($name==null){
return 'parameter is null';
}else{
return $name;
}
}
}
//resource route--------------------------------
// in resource you should use 's' end of name for example for 'post' we have:
Route:resource('posts','PostController'); // create all method
Route:resource('posts','PostController')-only(['show','edit','create']);
Route:resource('posts','PostController')-except(['show','edit']);
// for create controller with its methods
php artisan make:controller PostController --resource --model=Post
//apiresource route ---------------------------
Route::apiResource('post','PostController'); // this add all method except edit and create
// call reoute address------------------------
route('user.create');
// change name of route-----------------------
// in resource :
Route::resource('faq', 'ProductFaqController', [
'names' => [
'index' => 'faq',
'store' => 'faq.new',
// etc...
]
]);
// in simple route:
Route::get('/','HomeController@index')-name('home');
// and call it with :
route('home');
//parameter -----------------------------------
// in simple route we have :
Route::get('/{id}','HomeController@index');
class HomeController extends Controller{
public function index($id){
}
}
// or use function
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
//
});
// optional
Route::get('user/{name?}', function ($name = null) {
return $name;
});
// with post method
Route::post('/file','FileController@index');
class FileController extends Controller{
public function index(Request $request){
}
}
//** if you use resource note that: by default method show-update-desroy need a model like post for action
// and you should change it to id
public function edit(Post $post){} -> public function edit($id){}
Route::resource('posts','PostController')->parameters(['posts'=>'id']);
public function update(Request $request,$id)
public function destroy($id)
public function show($id){
$news = News::find($id);
return view('news.show',compact('news'));
}
public function edit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment