Search code examples
laravelcontrollerrouteslaravel-views

Automatically Load/Show Laravel View File


I am thinking of any techniques of autoloading the view files according to url.

For example:

public function addProducts()
{
    return view('admin.addProducts');
}

public function editProducts()
{
    return view('admin.editProducts');
}

public function allProducts()
{
    return view('admin.allProducts');
}

Here, the Controller's method name is identical to view file name. So, I am thinking, if it is possible to load the view files without writing same kind of method again and again.

Enlighten me.


Solution

  • If your route only needs to return a view, you may use the Route::view method.

    For example:

    Route::view('/welcome', 'welcome');
    Route::view('/welcome', 'welcome', ['name' => 'Taylor']);
    

    read more here