How do I pass a hard coded variable to a controller?
如何将硬编码变量传递给控制器?
My route is:
我的路线是:
Route::group(array('prefix' => $locale), function() {
Route::get('/milk', array('as' => 'milk', 'uses' => 'ProductsController@index'));
});
I want to do something like:
我想做的事情如下:
Route::get('/milk', array('as' => 'milk', 'uses' => 'ProductsController@index(1)'));
But that doesn't work.
但这不起作用。
How can this be done?
如何才能做到这一点?
Sorry if I have not explained well.
对不起,如果我没有解释清楚。
I wish to simply hardcode (set in stone by me) the type_id for certain routes like so:
我希望简单地硬编码(由我设置)某些路由的type_id如下:
Route::get('/milk', array('as' => 'milk', 'uses' => 'ProductsController@index(1)'));
Route::get('/cheese', array('as' => 'cheese', 'uses' => 'ProductsController@index(2)'));
...
My ProductsController for reference:
我的产品控制器供参考:
class ProductsController extends BaseController {
public function index($type_id) {
$Products = new Products;
$products = $Products->where('type_id', $type_id)->get();
return View::make('products.products', array('products' => $products));
}
}
24
You can use a closure for your route and then call the controller action:
您可以为路由使用闭包,然后调用控制器操作:
Route::get('/milk', array('as' => 'milk', function(){
return App::make('ProductsController')->index(1);
}));
However, a nicer way would be to use a where
condition and then do the type-to-id conversion in the controller. You will lose the direct alias though and would have to pass in the product as parameter when generating the URL.
但是,更好的方法是使用where条件,然后在控制器中进行type-to-id转换。您将失去直接别名,并且在生成URL时必须将产品作为参数传递。
Route::get('{product}', array('as' => 'product', 'uses' => 'ProductsController@index'))
->where('product', '(milk|cheese)');
3
I have used this to pass values to the controller...
我用它来将值传递给控制器......
route:
Route::get('user/{user}/usermanage', array('as' => 'userdata.usermanage', 'uses' => 'yourController@getUserDetails'));
//{user} - holds some value...
in controller:
public function getUserDetails($id)
{
...
}
if want dynamic :
如果想要动态:
$var = "Lists";
Route::get('something', array('as' => 'something', 'uses' => 'yourController@get'.$var));
hope this helps...
希望这可以帮助...
2
I feel like the tidiest way to do this is probably with route constraints:
我觉得这样做最干净的方法可能是路线限制:
Route::get('{milk}', [ 'as' => 'milk', 'uses' => 'ProductsController@index' ])
->where('milk', 'milk'); // matches the named arg {milk} (param 1)
// to the regex literal 'milk' (param 2)
It has some redundancy, but if you want to do it purely from your routes, I'd go with this.
它有一些冗余,但如果你想纯粹从你的路线上做,我会选择这个。
For making SEO-friendly names though, you could use Sluggable to generate a unique slug for each product, then create the following route:
为了制作SEO友好的名称,你可以使用Sluggable为每个产品生成一个独特的slug,然后创建以下路线:
Route::get('{product}', [ 'as' => 'product', 'before' => 'product-slug', 'uses' => 'ProductsController@index' ])
->where('product', '[a-z0-9]+[a-z0-9\-]*'); // valid slug syntax
And this filter:
而这个过滤器:
Route::filter('product-slug', function($route) {
$slug = $route->getParameter( 'slug' );
if (is_numeric($slug)) { // if the slug is an ID
$product = Product::findOrFail($slug); // try to find the product
return Redirect::route('product', $product->slug); // and redirect to it
}
});
0
Here is how you actually do it without messing up the url:
这是你如何实际做到这一点而不搞乱网址:
Define Route:
Route::match(['GET', 'POST'], 'my-url', ['var_1'=>'hello', 'var_2'=>'world', 'prefix'=>'my-prefix', 'middleware'=>['web', 'mid2', 'mid3'], 'as'=>"my-route-name", 'uses'=>'myController@index']);
Now in the controller, inside function __construct(Request $request)
:
现在在控制器中,内部函数__construct(Request $ request):
$req_action = @$request->route()->getAction();
$var_1 = $var_2 = '';
if(is_array($req_action) && !empty($req_action['var_1'])){
$var_1 = (int)@$req_action['var_1'];
}
if(is_array($req_action) && !empty($req_action['var_2'])){
$var_2 = @$req_action['var_2'];
}
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2014/11/10/f7d716e6fbdedddc3d74d235d95c7ea8.html。