【问题标题】:Should I re-use controllers in laravel between admin & api ? or have my admin consume my API?我应该在 admin 和 api 之间重用 laravel 中的控制器吗?还是让我的管理员使用我的 API?
【发布时间】:2014-01-10 13:29:37
【问题描述】:

刚接触 laravel 并试图找出构建我的应用程序的最佳方式。

它有一个管理界面和一个 API(JSON、angularjs 前端)。

我的路线目前看起来像:

Route::group(array('prefix' => 'admin', 'before' => 'auth.admin'), function()
{
    Route::any('/', array('as' => 'admin.index', function() {
        return View::make('admin.index');
    }));

    Route::resource('countries.products', 'ProductsController');

    Route::resource('countries', 'CountriesController');

    Route::resource('orders', 'OrdersController');

});

// Route group for API versioning
Route::group(array('prefix' => 'api/v1'), function()
{
    Route::resource('products', 'APIProductsController', array('only' => array('index', 'show')));

    Route::resource('orders', 'APIOrdersController', array('only' => array('store', 'update')));

});

OrdersController 和 APIOrdersController 中有很多重复的逻辑。我是否应该以某种方式重新使用单个控制器,也许是内容协商?还是修改 OrdersController 来查询 API 路由而不是使用 eloquent 更好?

或者还有其他更好的方法吗?

【问题讨论】:

    标签: php api rest laravel


    【解决方案1】:

    正如我所见,我会将所有对象创建逻辑提取到一个适当的类中(听起来像是repository 的一个很好的例子)。这个类应该只知道它必须接收的参数,并做出相应的响应。例如:

    class EloquentOrder implements OrderRepositoryInterface {
    
        // Instance of OrderValidator, 
        // assuming we have one
        protected $validator; 
    
        public function create($params)
        {
            // Pseudo-code
            $this->validator = new Ordervalidator($params);
            if ($this->validator->passes())
                create and return new Order
            else
                return validator errors
        }
    
    }
    

    然后,您的每个模块都可以在其控制器中使用此功能。

    在您的 API 中,您可以这样:

    class APIOrderController extends APIController {
    
        protected $repository;
    
        public function __construct(OrderRepositoryInterface $repository)
        {
            $this->repository = $repository;
        }
    
        public function create()
        {
            // Let's imagine you have an APIAuth class which 
            // authenticates via auth tokens:
            if (APIAuth::check()) {
                $params = Input::all();
                return $this->repository->new($params);
            }
    
            return Response::json(['error' => 'You are not authorized to create orders'], 401);
        }
    
    }
    

    在您的管理模块中,您可以:

    class AdminOrderController extends AdminController {
    
        protected $repository;
    
        public function __construct(OrderRepositoryInterface $repository)
        {
            $this->repository = $repository;
        }
    
        public function create()
        {
            // Now, let's imagine Auth uses a different authentication
            // method, and can check for specific permissions
            if (Auth::check() && Auth::hasPermission('create.orders')) {
                $params = Input::all();
                return $this->repository->new($params);
            }
    
            return Redirect::home()->with('message', 'You are not authorized to create orders');
        }
    
    }
    

    如您所见,这允许您在不同的上下文中重用您的对象创建逻辑。在示例中,我使用不同的身份验证方法和响应只是为了显示灵活性,但这实际上取决于您的项目要求。

    【讨论】:

    • 可能想要更改函数名称。 new 是保留字。公共函数 create() ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多