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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:55:52  来源:igfitidea点击:

How to create a resource route in laravel

phplaravellaravel-4

提问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 artisancommand from your terminal/command prompt

要创建资源丰富的控制器,您应该artisan从终端/命令提示符运行以下命令

php artisan controller:make HomeController

This will create a resourceful controller inside your controllersfolder, 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 7methods and to view the routes mapping for each method you may run the routescommand 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 urlor route namefor each actions/methods in your resourceful comtroller.

此命令将显示所有路由和操作/url 的表,并命名每个路由使用的名称,因此您可以在资源丰富的控制器中找到正确的urlroute name每个操作/方法。

If you declare a route using Route::controller('/', 'IndexController');then you have to create the IndexControllermanually and in this controller (RESTfull) you can create methods using httpverbs (GET, POSTetc) as prefix like

如果您使用声明路由,Route::controller('/', 'IndexController');则必须IndexController手动创建,并且在此控制器(RESTfull)中,您可以使用http动词(等)作为前缀创建方法GETPOST例如

public function getIndex()
{
    //
}

For this action/method, your url will be IndexControllerand it should be a normal GETrequest 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 POSTmethod 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 routescommand to find out the url/route namefor 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!

在你的终端!