扩展资源控制器 [英] Extend Resource Controllers

查看:45
本文介绍了扩展资源控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以某种方式做我想做的事,我正在寻找替代品或更好的做事方法.我在应用程序中使用资源控制器.另外,我在几种模型中使用softdelete,所以我的路线如下:

I'm doing what I want in a certain way and I'm looking for alternatives or better ways of doing. I'm using Resource Controllers in my application. Also I'm using softdelete in several models, so my routes are as follows:

Route::get('users/deleted', array('uses' => 'UserController@trash'));
Route::put('users/{id}/restore', array('uses' => 'UserController@restore'));
Route::resource('users', 'UserController');

第一个方法是显示已删除的对象.第二个允许我恢复这些已删除的元素.第三个映射了传统方法(创建,编辑,更新等).

The first route is to display objects that have been deleted. The second allows me to restore these deleted elements. The third maps the traditional methods (create, edit, update, etc.).

我有几个控制器的工作方式完全相同,我想知道是否有任何方法可以告诉laravel默认情况下使用这两种方法(废纸delete和删除),而无需另外两行.

I have several controllers that work exactly the same way, I wonder if have any way to tell laravel to work with this two methods (trash and delete) by default without the two extra lines.

有可能吗?还是提供一种更好的方式?(对不起,英语不好)

Is it possible? Or to give a better way that I'm doing? (sorry for bad english)

推荐答案

使事情变得简单而干燥.

您可以扩展Router并替换 app/config/app.php 文件中的Route Facade,但看起来工作量不大,但收益却不多,但请不要忘记您的路由该文件是一个PHP脚本,您可以执行以下操作:

Keeping things simple and DRY.

You can extend the Router and replace the Route Facade in your app/config/app.php file but seems like a lot of work for not much gain, but don't forget that your routes file is a PHP script and you can do things like:

$routes = [
    ['users' => 'UserController'],
    ['posts' => 'PostController'],
];

foreach ($routes as $key => $controller)
{
    Route::get("$key/deleted", array('uses' => "$controller@trash"));

    Route::put("$key/{id}/restore", array('uses' => "$controller@restore"));

    Route::resource($key, $controller);
}

扩展路由器

要扩展路由器,您需要创建3个类:

Extending the router

To extend the router you need to create 3 classes:

路由器扩展,您将在其中添加新方法:

The Router extended, where you'll add your new methods:

<?php namespace App\Routing;

class ExtendedRouter extends \Illuminate\Routing\Router {

    protected $resourceDefaults = array(
                'index', 
                'create', 
                'store', 
                'show', 
                'edit', 
                'update', 
                'destroy', 
                'deleted',
                'restore',
    );

    protected function addResourceDeleted($name, $base, $controller)
    {
        $uri = $this->getResourceUri($name).'/deleted';

        return $this->get($uri, $this->getResourceAction($name, $controller, 'deleted'));
    }

    protected function addResourceRestore($name, $base, $controller)
    {
        $uri = $this->getResourceUri($name).'/{resource}/restore';

        return $this->get($uri, $this->getResourceAction($name, $controller, 'restore'));
    }

}

服务提供商,使用Laravel使用的相同IoC标识符(路由器")来启动新路由器:

A Service Provider, to boot your new router, using the same IoC identifier Laravel uses ('router'):

<?php namespace App\Routing;

use Illuminate\Support\ServiceProvider;

class ExtendedRouterServiceProvider extends ServiceProvider {

    protected $defer = true;

    public function register()
    {
        $this->app['router'] = $this->app->share(function() { return new ExtendedRouter($this->app); });
    }

    public function provides()
    {
        return array('router');
    }

}

还有一个立面,以取代Laravel的立面

And a Facade, to replace Laravel's one

<?php namespace App\Facades;

use Illuminate\Support\Facades\Facade as IlluminateFacade;

class ExtendedRouteFacade extends IlluminateFacade {

    public static function is($name)
    {
        return static::$app['router']->currentRouteNamed($name);
    }

    public static function uses($action)
    {
        return static::$app['router']->currentRouteUses($action);
    }

    protected static function getFacadeAccessor() { return 'router'; }

}

然后,您需要将服务提供商和外观添加到 app/config/app.php 文件中,并注释Laravel原始文件.

Then you need to add your Service Provider and Facade to your app/config/app.php file, commenting the Laravel original ones.

这篇关于扩展资源控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆