如何使用 Artisan 命令 (PHP) 在 Laravel 5.2 中创建 RESTful 资源控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34677550/
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-08-26 00:09:25  来源:igfitidea点击:

How to create a RESTful Resource Controller in Laravel 5.2, using Artisan command (PHP)

phplaravel-5.2ioc-container

提问by Vicky

I'm working with Laravel 5 and I would like to know how to generate a RESTful Resource Controller with all predefined methods using the Artisan command (PHP).

我正在使用 Laravel 5,我想知道如何使用 Artisan 命令 (PHP) 生成具有所有预定义方法的 RESTful 资源控制器。

When I run php artisan make:controller LessonsController, it creates a controller, with no methods as shown below:

当我运行时php artisan make:controller LessonsController,它会创建一个控制器,没有如下所示的方法:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;

class LessonsController extends Controller
{


}

What I want to create is a complete Laravel RESTful Resource Controller with all predefined methods as in: index(), create(), store(), show(), edit(), update()and destroy().

我想要创建的是一个完整的 Laravel RESTful 资源控制器,其中包含所有预定义的方法,如:index(), create(), store(), show(), edit(), update()destroy()

How can I achieve this?

我怎样才能做到这一点?

回答by lagbox

Try getting help on the command

尝试获取有关命令的帮助

php artisan help make:controller

If you see a --resourceflag in the help options you are probably on 5.2 or newerand can add that flag to the command to get a resource controller.

如果您--resource在帮助选项中看到一个标志,您可能使用的是5.2 或更高版本,并且可以将该标志添加到命令中以获取资源控制器。

php artisan make:controller --resource SomeResourceController

For Laravel 5.0 and 5.1 the make:controllercommand would make a resource controller by default and the --plainoption would make a plain controller.

对于 Laravel 5.0 和 5.1,该make:controller命令将默认创建一个资源控制器,而该--plain选项将创建一个普通控制器。

Laravel 5.2 - Restful Resource Controllers- Default plain

Laravel 5.2 - Restful Resource Controllers- 默认普通

Laravel 5.1 - Restful Resource Controllers- Default resource

Laravel 5.1 - Restful Resource Controllers- 默认资源

Laravel 5.0 - Restful Resource Controllers- Default resource

Laravel 5.0 - Restful Resource Controllers- 默认资源

Summary: from Laravel 5.2 onward the make:controllerartisan command will create a plain controller by default.

总结:从 Laravel 5.2 开始,make:controllerartisan 命令默认会创建一个普通的控制器。

回答by Yogesh Yadav

For Laravel 5.2

对于Laravel 5.2

php artisan make:controller NameofController --resource
// It will create the controller with all methods.

If Laravel < 5.2

如果Laravel < 5.2

php artisan make:controller NameofController
// It will create the controller with all methods.

and

php artisan make:controller NameofController --plain
// It will create the controller without any method.

回答by Nihar

For default controller which have all methods you want. php artisan make:controller LessonsController

对于具有您想要的所有方法的默认控制器。 php artisan make:controller LessonsController

If you want plain controller with no method php artisan make:controller --plain LessonsController

如果你想要没有方法的普通控制器 php artisan make:controller --plain LessonsController

回答by MasterSith

php artisan make:controller "NameOfController"- will create controller with all methods

php artisan make:controller "NameOfController"- 将使用所有方法创建控制器

php artisan make:controller "NameOfController" --plain This will create controller with no methods.

Best Regards, I am using laravel 5.0

最好的问候,我正在使用 Laravel 5.0

回答by Hekmat

php artisan make:controller ControllerName --resource

回答by Vinod Tigadi

so you are using Laravel 5.2, so to have the controller with RESTful methods issue the command

所以你使用的是 Laravel 5.2,所以要让带有 RESTful 方法的控制器发出命令

php artisan make:controller --resource NAME_OF_CONTROLLER

In Laravel 5.1 and below, by default the make:controllercommand used to generate the Controller with all required methods such as 'index, create, store, show, edit, update, destroy'. And for 5.1 and below, to have the blank controller file without any methods, we used to use '--plain' parameter as

在 Laravel 5.1 及以下版本中,默认情况下make:controller命令用于生成具有所有必需方法的控制器,例如“索引、创建、存储、显示、编辑、更新、销毁”。对于 5.1 及以下版本,为了获得没有任何方法的空白控制器文件,我们曾经使用 '--plain' 参数作为

php artisan make:controller --plain NAME_OF_CONTROLLER

But with Laravel 5.2, by default the artisan command will create the bare controller file without any RESTful methods.

但是在 Laravel 5.2 中,默认情况下 artisan 命令将创建没有任何 RESTful 方法的裸控制器文件。

As Laravel 5.2 has many changes, it is better to use the 'artisan help' command as below

由于 Laravel 5.2 有很多变化,最好使用“工匠帮助”命令,如下所示

php artisan help make:controller

With this, we will realize the introduction of --resource

有了这个,我们将实现引入 --resource

Please Refer the Laravel Documentation Laravel HTTP Controllers - Artisan Command

请参阅 Laravel 文档Laravel HTTP 控制器 - Artisan 命令

Suggestion: As this is the change from 5.2, it would be good to edit the Post Title too.

建议:由于这是5.2的变化,所以最好也编辑帖子标题。