How to create resource controller in Laravel 4.2

Laravel 4.2 have predefined resource controller that you can use to start develop CRUD (Create, Read, Update, Delete) based application quickly.

First you need to generate the controller using artisan command, so fire up your command line and run this:
php artisan controller:make [name-of-the-controller]
php artisan controller:make UserController
php artisan controller:make StudentController
php artisan controller:make SuperCarController
Note: if you are getting error message 'permission denied', try to run it with 'sudo' command.

Take a look at your app/controllers folder, you should be able to see the new controller that just generated. Inside you can see the predefined methods generated by laravel, you can put your application code in there.

You have a controller and a bunch of methods in there, next you need to create the routes for each of those methods, you don't need to create one by one, that's so boring, laravel can do it for us.

To create routes for all methods on the predefined resource controller, we can use the Route::resource() and it will create all routes for our resource controller.

Route::resource('student', 'StudentController');

That's it, all methods in the resource controller have it's own routes now, if you don't believe me, go ahead run 'php artisan routes' to see the list of routes on your laravel application.

No comments :

Post a Comment

Leave A Comment...