如何在 Laravel Voyager 中创建自定义控制器

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

How to create custom controller in Laravel Voyager

phplaravellaravel-5voyager

提问by raff

I am very new in Voyager.

我是航海者的新手。

I have got all the controllers inside TCG\\Voyager\\Http\\Controllerswhile installing Voyager but didn't find other controllers those I have created using BREAD.

TCG\\Voyager\\Http\\Controllers在安装 Voyager 时已经将所有控制器都安装在里面,但没有找到我使用 BREAD 创建的其他控制器。

Besides that I want to create custom controller in my Voyager admin panel inside App\\Http\\Controllers\\Voyager. I also followed the steps of Voyager tutorial in Youtube for making custom controller, but couldn't create.

除此之外,我想在我的 Voyager 管理面板中创建自定义控制器App\\Http\\Controllers\\Voyager。我也按照 Youtube 中 Voyager 教程的步骤制作自定义控制器,但无法创建。

Anybody help please ?

有人帮忙吗?

回答by FloatingKiwi

In your config\voyager.php file add your namespace:

在您的 config\voyager.php 文件中添加您的命名空间:

'controllers' => [
    'namespace' => 'App\Http\Controllers\Back',
],

Then publish voyageres controllers to your namespace

然后将 voyageres 控制器发布到你的命名空间

php artisan voyager:controllers

Within that namespace create a new controller derived from VoyagerBreadController

在该命名空间内创建一个派生自 VoyagerBreadController 的新控制器

namespace App\Http\Controllers\Back;

use Illuminate\Http\Request;

class SchoolController extends VoyagerBreadController
{

Then you can specify the controller in the bread editor.

然后你可以在面包编辑器中指定控制器。

NOTE: I did have to refer to mine as Back\SchoolController instead of just SchoolController as I would have expected.

注意:我确实必须将我的称为 Back\SchoolController 而不是我所期望的 SchoolController。

Setting controller on backend

在后端设置控制器

回答by Gourav Pathela

Update: From version 1.1 now you need to extend VoyagerBaseControllerinstead of VoyagerBreadController.

更新:从 1.1 版开始,您现在需要扩展VoyagerBaseController而不是VoyagerBreadController.

回答by Stoney Eagle

Add this to your model.

将此添加到您的模型中。

use Illuminate\Database\Eloquent\Builder;

use Illuminate\Database\Eloquent\Builder;

protected static function boot()
    {
        parent::boot();
        static::addGlobalScope('order', function (Builder $builder) {
            $builder->orderBy('name', 'asc');
        });
}