简体   繁体   English

我应该在admin和api之间的laravel中重新使用控制器吗? 或让我的管理员使用我的API?

[英]Should I re-use controllers in laravel between admin & api ? or have my admin consume my API?

New to laravel and trying to work out the best way to structure my app. laravel的新手,并试图找出构建我的应用程序的最佳方法。

It has both an admin interface and an API (JSON, angularjs front-end). 它有一个管理界面和一个API(JSON,angularjs前端)。

my routes currently look like: 我的路线目前看起来像:

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')));

});

There is a lot of duplicated logic in eg, the OrdersController & APIOrdersController. 在OrdersController和AP​​IOrdersController中有很多重复的逻辑。 Should I re-use a single controller somehow, maybe with content-negotation? 我应该以某种方式重新使用单个控制器,也许是通过内容协商? or is it better to modify OrdersController to query the API routes instead of using eloquent? 或者更好的是修改OrdersController以查询API路由而不是使用eloquent?

or is there another, better way? 或者还有另一种更好的方法吗?

As I see it, I would extract all object creation logic to a proper class (sounds like a good case for a repository ). 在我看来,我会将所有对象创建逻辑提取到一个合适的类(听起来像一个存储库的好例子)。 This class should only know about the parameters it has to receive, and respond accordingly. 这个类应该只知道它必须接收的参数,并做出相应的响应。 For example: 例如:

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
    }

}

Then, each of your modules can use this functionality inside its controllers. 然后,您的每个模块都可以在其控制器中使用此功能。

In your API, you could have this: 在您的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);
    }

}

While in your administration module, you could have: 在管理模块中,您可以:

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');
    }

}

As you can see, this allows you to reuse your object creation logic in different contexts. 如您所见,这允许您在不同的上下文中重用对象创建逻辑。 In the example I've used different authentication methods and responses just to show flexibility, but this will really depend on your project requirements. 在示例中,我使用了不同的身份验证方法和响应来表示灵活性,但这实际上取决于您的项目要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM