Laravel dynamic route

0

I have the following question. Is it correct to create a dynamic route to return my views, or do I have to create a route for each controller and return the proper view?

Example of the code I'm using

route file

Route::get('/{url?}/{url2?}/{url3?}', 'UrlController@getUrl');

controller

public function getUrl($url = null, $url2 = null, $url3 = null) {
    if (isset($url3)) {
        return view($url . '.' . $url2 . '.' . $url3);
    } elseif (isset($url2)) {
        return view($url . '.' . $url2);
    } elseif (isset($url)) {
        return view($url, ['name' => 'Romullo']);
    } else {
        return view('home');

    }
}

This code returns me exactly the view that is typed in the url or generates the error page correctly. But the doubt arose among so many articles that I read. Is that correct or not? Should I actually create route by route for each controller and view or can I dynamically call?

    
asked by anonymous 09.10.2018 / 23:13

1 answer

0

I do not know exactly what you want to do with this code. I think you'd better do separate routes. Depending on what you want to do it would be easier until you create a factory with this dynamic route and use that factory to send the guy to the controller controller.

The way you're doing your code is going to get complex as you get older and it's going to be harder to maintain it and for other people to contribute as well.

    
10.10.2018 / 19:09