Reputation: 3596
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.
Upvotes: 4
Views: 4241
Reputation: 1613
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.
Upvotes: 6
Reputation: 3416
You can access all of the same classes from inside of a controller that you can access inside of an anonymous function attached to the route.
For example, if you have this route:
Route::post('register', function(){
$user = new User();
$user->username = Input::get('username');
$user->password = Hash::make(Input::get('password'));
$user->save();
return Redirect::to('home');
});
You could replace it with this:
Route::post('register', array('uses' => 'auth@register'));
class Auth_Controller extends Base_Controller {
public static $restful = true;
public function post_register() {
$user = new User();
$user->username = Input::get('username');
$user->password = Hash::make(Input::get('password'));
$user->save();
return Redirect::to('home');
}
}
... and get the same result, but with a little more organization which becomes especially important as your application grows.
Upvotes: 4