laravel 如何在laravel中创建资源路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21409166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a resource route in laravel
提问by manuthalasseril
I create a controller named HomeControllerand their methods named
我创建了一个名为HomeController的控制器,它们的方法名为
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
更新
感谢大家的帮助.. 我是 Laravel 的初学者。所以我在路由中犯了一个大错误,我忘记了先进先出规则。所以我必须重新排序我的路线
Route::controller('/', 'IndexController');
Route::resource('homes', 'HomesController');
to
到
Route::resource('homes', 'HomesController');
Route::controller('/', 'IndexController');
回答by Miroslav Trninic
If you are creating controller manually check out that you extend BaseController class.
如果您手动创建控制器,请检查您是否扩展了 BaseController 类。
You can install https://github.com/JeffreyWay/Laravel-4-Generators
你可以安装https://github.com/JeffreyWay/Laravel-4-Generators
Jeffrey Way's Laravel command line utilities and with one command:
Jeffrey Way 的 Laravel 命令行实用程序和一个命令:
php artisan generate:resource
You'll have complete REST-ful resource generated - from routes to models and controllers and even migration table.
您将生成完整的 REST-ful 资源 - 从路由到模型和控制器,甚至迁移表。
回答by The Alpha
To create a resourceful controller you should run the following artisan
command from your terminal/command prompt
要创建资源丰富的控制器,您应该artisan
从终端/命令提示符运行以下命令
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
这将在您的controllers
文件夹中创建一个资源丰富的控制器,如果需要,您可以提供一个路径作为命令的选项,例如
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
它将使用7
方法创建一个资源丰富的控制器,并查看每个方法的路由映射,您可以routes
从终端/命令提示符运行命令,例如
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.
此命令将显示所有路由和操作/url 的表,并命名每个路由使用的名称,因此您可以在资源丰富的控制器中找到正确的url
或route name
每个操作/方法。
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
如果您使用声明路由,Route::controller('/', 'IndexController');
则必须IndexController
手动创建,并且在此控制器(RESTfull)中,您可以使用http
动词(等)作为前缀创建方法GET
,POST
例如
public function getIndex()
{
//
}
For this action/method, your url will be IndexController
and it should be a normal GET
request from address bar.
对于此操作/方法,您的 url 将是地址栏IndexController
的正常GET
请求。
To submit a form you may declare a post method like:
要提交表单,您可以声明一个 post 方法,例如:
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.
因此,您可以使用表单中的方法向此操作/方法提交表单,POST
表单的操作将是http://example.com/save
,而无需动词。您始终可以运行该php artisan routes
命令来找出url/route name
控制器方法/功能的 . 有关更多信息,请查看文档。
回答by Chris G
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:
如果您使用的是资源控制器,则只需要“索引”方法。给定您的路线,您必须输入:yourdomain.com/home 才能访问 Home Controller 的索引。如果您想使用与主页相同的索引方法,您需要将其添加到您的路由文件中:
Route::get('/', 'HomeController@index');
Also, make (if you haven't already) run:
另外,使(如果您还没有)运行:
composer dump-autoload
回答by jody_lognoul
You have to do this as well :
你也必须这样做:
php artisan controller:make HomeController
In your terminal!
在你的终端!