I create a controller named HomeController and their methods named
getIndex()
and
index()
and after that i make a routing is follows
Route::resource('home', 'HomeController');
But it will gives me this error
Controller method not found.
I have fine working in this routes
Route::controller('/', 'IndexController');
Please help
Update
Thanks all for your help.. I am a beginner to laravel. So I made a big mistake in my routing that I forget about first in first out rule. So I have to reorder my routes from
Route::controller('/', 'IndexController');
Route::resource('homes', 'HomesController');
to
Route::resource('homes', 'HomesController');
Route::controller('/', 'IndexController');
If you are using a resource controller, you will only need the "index" method. Given your route, you would have to enter: yourdomain.com/home in order to get to the index of Home Controller. If you would like to use the same index method as the home page you would need to add this to your routes file:
Route::get('/', 'HomeController@index');
Also, make (if you haven't already) run:
composer dump-autoload
You have to do this as well :
php artisan controller:make HomeController
In your terminal!
If you are creating controller manually check out that you extend BaseController class.
You can install https://github.com/JeffreyWay/Laravel-4-Generators
Jeffrey Way's Laravel command line utilities and with one command:
php artisan generate:resource
You'll have complete REST-ful resource generated - from routes to models and controllers and even migration table.
To create a resourceful controller you should run the following artisan
command from your terminal/command prompt
php artisan controller:make HomeController
This will create a resourceful controller inside your controllers
folder, if you want, you may provide a path as an option of the command like
php artisan controller:make HomeController --path="app/controllers/admin"
It'll create a resourceful controller with 7
methods and to view the routes mapping for each method you may run the routes
command from terminal/command prompt like
php artisan routes
This command will show the table of all routes and action/url and name each route uses, so you can find out the proper url
or route name
for each actions/methods in your resourceful comtroller.
If you declare a route using Route::controller('/', 'IndexController');
then you have to create the IndexController
manually and in this controller (RESTfull) you can create methods using http
verbs (GET
, POST
etc) as prefix like
public function getIndex()
{
//
}
For this action/method, your url will be IndexController
and it should be a normal GET
request from address bar.
To submit a form you may declare a post method like:
public function postSave()
{
//
}
So, you can submit a form to this action/method using POST
method in the form and the action for the form would be http://example.com/save
, without the verb. You can always, run the php artisan routes
command to find out the url/route name
for a controller method/function. For more information, check the documentation.