0

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.

Delowar Hossain
  • 375
  • 1
  • 3
  • 19

2 Answers2

1

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

Kapitan Teemo
  • 2,144
  • 1
  • 11
  • 25
-1

It's the call PHP magic, man. https://repl.it/@Piterden/PHP-call-magic?language=php

public function __call($method, $parameters)
{
    if (str_contains($method, 'Product')) {
        return view("admin.{$method}");
    }
}

btw, it's not a good practice for controller.

Piterden
  • 771
  • 5
  • 17
  • Thank you for the code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its [long-term value](https://meta.stackexchange.com/q/114762/206345) by describing why this is a good solution to the problem, and would make it more useful to future readers with other similar questions. Please edit your answer to add some explanation, including the assumptions you've made. – sepehr Oct 24 '18 at 15:00
  • 1
    Updated as you wish – Piterden Oct 24 '18 at 21:51