Home Laravel laravel Route Methods

laravel Route Methods

by therichpost
Published: Updated: 0 comments
Laravel 7.2 routing with route group auth guard check with prefix

Hello to all, welcome to therichpost.com. In this post, I will tell you, laravel Route Methods. I am doing with laravel first time in my website. Laravel is one of the top php mvc framework. I personally like laravel routes feature very much. Here are the some important laravel Route Methods, with them we can do more with laravel. you can write these methods into routes/web.php file:

1. Simple Method To return view to particular url:

Route::get(‘map’, function(){
return view(‘map’);
});

 2:Sending emails with Mailgun and Laravel is easy!

Route::get(‘/send-test-email’, function(){
Mail::raw(‘Sending emails with Mailgun and Laravel is easy!’, function($message)
{
$message->subject(‘Mailgun and Laravel are awesome!’);
// $message->from(‘no-reply@website_name.com’, ‘Website Name’);
$message->to(‘therichposts@gmail.com’);
});

Mail::to(‘therichposts@gmail.com’)->send(new App\Mail\CustomerWelcomeMessage());
});

 3:Sending simple emails with Laravel is easy!

Route::get(‘/added-new-product-mail’, function(){
Mail::to(‘therichposts@gmail.com’)->send(new App\Mail\AddNewProduct($product));
return view(’emails.admin.addednewproduct’);
});

 4:redirection in laravel routes!

Route::get(‘/test-page’, function () {
return Redirect::to(‘/blog/test-page’, 301); 
});

 5. Custom Routes:

Route::view('/', 'home'); // Home page
Route::view('/about', 'about'); // About Page
Route::view('/contact', 'contact'); // Contact Page

 6. Routing with parameters:

Route::get('user/{id}', function ($id) {
  return 'User '.$id;
});

 7. Routing with regular expressions:

Route::get('user/{name}', function ($name) {
  //
})->where('name', '[A-Za-z]+'); // only alphabets accepted for name parameter
Route::get('user/{id}', function ($id) {
  //
})->where('id', '[0-9]+'); // only digits accepted for id parameter
Route::get('user/{id}/{name}', function ($id, $name) {
  //
})->where(['id' => '[0-9]+', 'name' => '[a-z]+']); // multiple parameter with required expressions

 8. Auth check routing:

Route::group(
  [
    'prefix' => 'admin',
      'middleware => [
      'auth',
      'roleAdmin'
    ]'
  ], function() {
  Route::get('/admindashboard', 'AdminController@admindashboard');
});

 9. Match Method Routing:

Route::match([methods], $uri, $callback);
Route::match(['get','post'],'/profile','admin@dashboard');
Route::any('bar', function () {
  //
});

 There are so many code tricks in laravel and i will let you know all. Please do comment if you any query related to this post. Thank you. Therichpost.com

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.