Routes in Laravel – With example

routes laravel

What is Routing

  • Routing is the one  of the main concept of laravel. Route is a way of create URL for your application. 
  • Routes is the web URL of your application

#Default route of laravel application:-

    <?php

    use Illuminate\Http\Request;

    use Illuminate\Support\Facades\Route;

    /*

    |--------------------------------------------------------------------------

    | API Routes

    |--------------------------------------------------------------------------

    |

    | Here is where you can register API routes for your application. These

    | routes are loaded by the RouteServiceProvider within a group which

    | is assigned the "api" middleware group. Enjoy building your API!

    |

    */

    Route::get('/', function () {

        return view('welcome');

    });
  • Inside our application, there is a web.php file in the routes folder in which Routes is defined.
  • Routes in Laravel are defined with methods :
                      get
                      post
put delete

 

#Routing with controller function

  • Step 1 : – Create controller and define route in controller
    <?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;

    class PostController extends Controller

    {

        public function index()

        {

            return view('index'); // route

        }

    }

#routes/web.php

  • Step 2 :- Create routes in web.php file
Route::get('/', [App\Http\Controllers\PostController::class, 'index'])->name('index');

                             // Or

Route::get('/', 'PostController@index')->name('index');

#Namig Routes :

Route::get('/', 'Admin\ServiceController@index')->name('index');

Leave a Comment

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