php - 作为 Controller 的 Laravel 服务 - 使用多个 Controller

标签 php laravel laravel-5

我是一名巴西开发者,所以...抱歉我的英语水平有限。

好吧,事实上我的问题更像是一个约定问题,因为直到现在我还没有使用 Laravel 的服务(到目前为止我的应用程序非常简单)。

我在问这个问题之前读过它,但对这种特定情况没有任何帮助。我会尽量客观地描述。

在此之前,只是评论:我知道在这些示例中只使用 Controller 的错误。这个问题实际上是关于那个错误的。

嗯,实际结构是:

abstract class CRUDController extends Controller {

    protected function __construct($data, $validatorData) {
        // store the data in a attribute
        // create with Validator facade the validation and store too
    }

    abstract protected function createRecord();

    protected function create() {
        try {
            // do the validation and return an Response instance with error messages
            // if the data is ok, store in the database with models
            // (here's where the magic takes place) in that store!
            // to do that, calls the method createRecord (which is abstract)
            $this->createRecord();
            // return a success message in an Response instance
        }
        catch(\Exception $e) {
            // return an Response instance with error messages
        }
    }

}

class UserController extends CRUDController {

    public function __construct($data) {
        parent::__construct($data, [
            'rules' => [
                 // specific user code here
             ],
            'messages' => [
                 // specific user code here
             ],
            'customAttributes' => [
                 // specific user code here
             ]
        ]);
    }

    protected function createRecord() {
        $user = new UserModel();
        // store values here...
        $user->save();
        return $user;
    }

}

// here's the route to consider in that example
Route::post('/user', 'WebsiteController@register');

class WebsiteController extends Controller {

    private $request;

    public function __construct(Request $request) {
        $this->request = $request;
    }

    public function register() {
        $user = new UserController();
        $user->create($this->request);
        // here's the problem: controller working with another controller
    }

}

class UserAPIController extends Controller {
    // use here the UserController too
}

以及许多其他以相同方式扩展 CRUDController 的类...

我想要什么

我想创建一个 Controller (这里称为 CRUDController)来重用模式所说的方法(创建、读取、更新和删除)。 为了真正客观,我将使用 create 方法作为示例。 通过上面的代码,目的似乎很清楚?我想是的......我所有的 Controller 都具有相同且可重复使用的验证代码。就是这样。 除此之外,我希望我的网站路由调用另一个 Controller (UserController) 来存储新用户......但以同样的方式,我将创建一个以相同方式(通过验证等)使用相同 Controller 的 API。这就是 CRUDController 中 Response 的目的(我将在 WebSiteController 中阅读它们以决定要做什么,比如显示 View ,另一方面,我基本上会通过 API 返回 Response。

我真正的问题

惯例和模式。 MVC 模式在这里被打破了。 Controller 调用另一个 Controller 是错误的,我知道。 我想知道我应该用什么东西!服务?是对的吗?我看到了很多(真的)服务示例,但没有类似的例子,使用模型和重用代码等。我从不使用服务,但我知道如何使用,但我不知道它是否适合这些情况。

我真的希望有人能在这里提供帮助,再次对英文的错误表示歉意。非常感谢。

最佳答案

您将 CRUD Controller 称为 Controller ,但它的行为并不像 MVC Controller 。充其量它只是一个辅助类。你总是可以这样做:

abstract class CRUDManager {        
    //As you had the CRUDController
}

class UserManager extends CRUDManager {
     //As you had the UserController
}

在您的 AppServiceProvider 中:

public function boot() {
     $app->bind(UserManager::class, function ($app) {
           return new UserManager(request()->all()); //I guess that's what you need.
     });
}

当你需要使用它时,你可以:

public function register(UserManager $user) {
    $user->create();
}

现在要指出一件事。在构造函数中初始化请求不是一个好主意。您应该在 Controller 方法中使用依赖注入(inject)。我什至不知道在构造 Controller 时请求是否可用(我知道 session 不可用)。之所以这么说,是因为中间件是在controller构造完成之后运行的,所以在调用controller方法的时候request可能会被修改。

另外注意:如果你做原来的解决方案是因为你需要使用某些 Controller 方法,那么你可以使用相应的特征(因为 Controller 本身并没有很多方法)。我猜像 ValidatesRequests 这样的特征将是您需要使用的。

关于php - 作为 Controller 的 Laravel 服务 - 使用多个 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43420699/

相关文章:

php - 行未被删除

forms - Laravel 5 动态表单验证

php - Laravel Eloquent 模型::查找不起作用

PHP 函数作用域失败

php - 在 ResultSet Zend Framework 2 的查询中执行?

php - 如何访问 Laravel 中某些特定 Controller 的数据?

php - Laravel 电子邮件模板样式

laravel - Openshift 中的 "Caught SIGWINCH, shutting down gracefully"错误

php - 在 Laravel 的不同上下文中使用 "use"关键字背后的概念是什么?

php - 查询元素列表