如何在Laravel中调用控制器操作并运行验证请求

时间:2016-06-07 13:37:37

标签: php laravel laravel-5

我的数据库中有一个groups表,每个组都有一个slug。我在routes.php文件中最后定义了以下路由,这样如果没有其他URL匹配,应用程序将检查slug是否属于某个组并显示组页面。组页面上还有一个表单,因此也需要处理此表单的提交。

Route::get('{slug}', ['as' => 'dynamic_route', function($slug){
    $group = \App\Group::where('slug', $slug)->first();
    if(!is_null($group)) {
        $app = app();
        $controller = $app->make('App\Http\Controllers\GroupsController');
        return $controller->callAction('view', ['slug' => $group->slug]);
    } else {
        abort(404);
    }
}]);

Route::post('{slug}', ['as' => 'dynamic_route_submit', function($slug){
    $group = \App\Group::where('slug', $slug)->first();
    if(!is_null($group)) {
        $app = app();
        $controller = $app->make('App\Http\Controllers\GroupsController');
        return $controller->callAction('handle_register', [$group->slug]);
    } else {
        abort(404);
    }
}]);

这是我的群组控制器:

<?php namespace App\Http\Controllers;
use View;
use App\Group;
use App\Lifestyle_question;
use App\Http\Requests\User\RegisterStep1Request;
use App\Http\Requests\User\RegisterStep2Request;
use Breadcrumbs;

class GroupsController extends FrontendController {

    public function __construct()
    {
        parent::__construct();
    }

    function view($slug)
    {
        $this->data['group'] = Group::where('slug', '=', $slug)->first();

        $this->data['lifestyle_questions'] = Lifestyle_question::all();

        Breadcrumbs::setCurrentRoute('dynamic_route', $this->data['group']);

        return View::make('groups/view', $this->data);
    }

    function handle_register(RegisterStep1Request $request1, RegisterStep2Request $request2, $slug)
    {
        $this->data['group'] = Group::where('slug', '=', $slug)->first();

        die("Validation passed");
    }
}

view方法正常但是当我提交表单时,我收到以下错误消息:

ErrorException in GroupsController.php line 27:
Argument 1 passed to App\Http\Controllers\GroupsController::handle_register() must be an instance of App\Http\Requests\User\RegisterStep1Request, string given

我知道这与从路由定义传递给控制器​​方法的参数有关,因此我尝试了以下方法来尝试对其进行排序:

Route::post('{slug}', ['as' => 'dynamic_route_submit', function($slug){
    $group = \App\Group::where('slug', $slug)->first();
    if(!is_null($group)) {
        $app = app();
        $controller = $app->make('App\Http\Controllers\GroupsController');
        return $controller->callAction('handle_register', [new \App\Http\Requests\User\RegisterStep1Request, new \App\Http\Requests\User\RegisterStep2Request, $group->slug]);
    } else {
        abort(404);
    }
}]);

这解决了问题,除了请求没有被触发。如何调用此方法并确保触发请求以便运行验证?

2 个答案:

答案 0 :(得分:1)

如果您要在其中调用控制器,请不要在路由中使用匿名功能。声明你的路线:

Route::post('{slug}', ['as' => 'dynamic_route_submit', 'uses' => 'App\Http\Controllers\GroupsController@handle_register']);

然后在控制器句柄中进行必要的验证。

答案 1 :(得分:-1)

您可以尝试将请求验证移出Request类并进入私有控制器操作:

<强> UserController.php

/**
 * Validates a Create User request
 */
protected function validateCreate()
{
    $this->validate($this->request, [
        'name'              => 'required|max:255',
        'email'             => 'required|unique:users|max:255',
        'account_types_id'  => 'required|numeric',
    ]);
}

请执行与您的代码类似的操作,并在控制器操作中调用这些验证方法:

<强> UserController.php

/**
 * @return \Illuminate\Http\RedirectResponse
 * @throws CreateException
 */
public function create()
{
    $this->validateCreate();

作为FYI,您可以使用request()->route()->getParameter('slug')

按名称访问路线参数
$slug = request()->route()->getParameter('slug');
$this->data['group'] = Group::where('slug', '=', $slug)->first();