phpresturl-routinglaravel

Route vs. Controller in Laravel


I was wondering about the best approach for laravel framework development. Most of the time and tutorials i go through, e.g: Laravel CodeHappy by Dayle, example as what you see here is the same with his book. Most Q&A in stack overflow, i noted most of developers also put all the request handling on route.php. May i know is this great for cloud kind of big application code structure? In my concept, RESTful was designed for api while we can use controller for detect it's get, post, put, or delete. like this

if ($_POST)
    {
       // Try and login user
    }
    else
    {
       // Show login form
    }

but there're not much of tutorials/samples available. Am i getting wrong concept? If changing everything to controller, i got no idea on how to do it. like the validation & getting input from controller. Is anyone have idea on this? Please advice. if example showing up would be the best ;) Thanks.


Solution

  • There really is no precise answer to that question. Whether you use routes or controllers (or both, which is perfectly acceptable and what most people are doing), depends on your application. Generally speaking, if your routes seem to have lots of business logic you should probably consider to "transform" them into controllers, as controllers are easier to maintain and overlook, especially for larger applications.

    In the end, it's probably best to combine the flexibility of routes with the power of controllers, for example by calling a controller using a route:

    Route::get('welcome', 'home@index');
    

    Here you call the index action on the home controller.

    This is a nice article if you want to read more about the Route vs. Controller debate.