将固定变量从路径传递到Laravel中的控制器

时间:2015-04-29 03:32:22

标签: php laravel laravel-4 laravel-5 laravel-routing

我试图通过我的路径将一个变量传递给我的控制器,但我有多条路径(类别)通向同一个控制器,即

Route::get('/category1/{region}/{suburb?}', 'SearchController@search');
Route::get('/category2/{region}/{suburb?}', 'SearchController@search');

将/ category1,2等作为参数/ {category}不是一个选项,我不想为每个类别制作单独的控制器功能。

如何将网址的第一段发送到我的搜索控制器?即category1或category2?

目前控制器如下:

public function search($region, $suburb = null) { }

谢谢!

1 个答案:

答案 0 :(得分:4)

您可以为{category}参数指定一个遮罩,使其必须符合格式"类别[0-9] +"为了匹配路线。

Route::get('/{category}/{region}/{suburb?}', 'SearchController@search')
    ->where('category', 'category[0-9]+');

现在,您的示例网址(来自评论)www.a.com/var1/var2/var3只会在var1与给定类别正则表达式匹配时匹配路线。

更多信息可以在路线参数here的文档中找到。

修改

是的,这可以使用一组字符串值。它是一个正则表达式,所以你只需要将你的字符串值数组放入该上下文中:

Route::get('/{category}/{region}/{suburb?}', 'SearchController@search')
    ->where('category', 'hairdresser|cooper|fletcher');

或者,如果您在其他地方构建了数组:

$arr = ['hairdresser', 'cooper', 'fletcher'];

// run each array entry through preg_quote and then glue
// the resulting array together with pipes
Route::get('/{category}/{region}/{suburb?}', 'SearchController@search')
    ->where('category', implode('|', array_map('preg_quote', $arr)));

编辑2(原始请求的解决方案)

您的原始问题是如何将硬编码的类别段传递到控制器中。如果由于某种原因,您不希望使用上述解决方案,则还有其他两种选择。

选项1:不传递该值,只需访问控制器中的请求段即可。

public function search($region, $suburb = null) {
    $category = \Request::segment(1);
    dd($category);
}

选项2:使用前置过滤器(L4)或中间件(L5)之前修改路由参数。

在过滤器(和中间件)有权访问路由对象之前,可以使用路由对象上的方法来修改路由参数。这些路由参数最终传递到控制器操作中。路径参数存储为关联数组,因此在尝试使订单正确时需要牢记这一点。

如果使用Laravel 4,则需要使用之前的过滤器。定义要使用前置过滤器的路由,并传入要添加到参数上的硬编码值。

Route::get('/hairdresser/{region}/{suburb?}', ['before' => 'shiftParameter:hairdresser', 'uses' => 'SearchController@search']);
Route::get('/cooper/{region}/{suburb?}', ['before' => 'shiftParameter:cooper', 'uses' => 'SearchController@search']);
Route::get('/fletcher/{region}/{suburb?}', ['before' => 'shiftParameter:fletcher', 'uses' => 'SearchController@search']);

Route::filter('shiftParameter', function ($route, $request, $value) {
    // save off the current route parameters    
    $parameters = $route->parameters();
    // unset the current route parameters
    foreach($parameters as $name => $parameter) {
        $route->forgetParameter($name);
    }

    // union the new parameters and the old parameters
    $parameters = ['customParameter0' => $value] + $parameters;
    // loop through the new set of parameters to add them to the route
    foreach($parameters as $name => $parameter) {
        $route->setParameter($name, $parameter);
    }
});

如果使用Laravel 5,您需要在中间件之前定义一个新的。将新类添加到app / Http / Middleware目录并将其注册到app / Http / Kernel.php中的$routeMiddleware变量中。逻辑基本相同,需要额外的箍才能将参数传递给中间件。

// the 'parameters' key is a custom key we're using to pass the data to the middleware
Route::get('/hairdresser/{region}/{suburb?}', ['middleware' => 'shiftParameter', 'parameters' => ['hairdresser'], 'uses' => 'SearchController@search']);
Route::get('/cooper/{region}/{suburb?}', ['middleware' => 'shiftParameter', 'parameters' => ['cooper'], 'uses' => 'SearchController@search']);
Route::get('/fletcher/{region}/{suburb?}', ['middleware' => 'shiftParameter', 'parameters' => ['fletcher'], 'uses' => 'SearchController@search']);

// middleware class to go in app/Http/Middleware
// generate with "php artisan make:middleware" statement and copy logic below
class ShiftParameterMiddleware {
    public function handle($request, Closure $next) {
        // get the route from the request
        $route = $request->route();

        // save off the current route parameters
        $parameters = $route->parameters();
        // unset the current route parameters
        foreach ($parameters as $name => $parameter) {
            $route->forgetParameter($name);
        }

        // build the new parameters to shift onto the array
        // from the data passed to the middleware
        $newParameters = [];
        foreach ($this->getParameters($request) as $key => $value) {
            $newParameters['customParameter' . $key] = $value;
        }

        // union the new parameters and the old parameters
        $parameters = $newParameters + $parameters;
        // loop through the new set of parameters to add them to the route
        foreach ($parameters as $name => $parameter) {
            $route->setParameter($name, $parameter);
        }

        return $next($request);
    }

    /**
     * Method to get the data from the custom 'parameters' key added
     * on the route definition.
     */
    protected function getParameters($request) {
        $actions = $request->route()->getAction();
        return $actions['parameters'];
    }
}

现在,通过设置和使用过滤器(或中间件),类别将作为第一个参数传递给控制器​​方法。

public function search($category, $region, $suburb = null) {
    dd($category);
}