Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel-使用';其中';在routes.php中查询模型_Php_Laravel_Laravel Routing - Fatal编程技术网

Laravel-使用';其中';在routes.php中查询模型

Laravel-使用';其中';在routes.php中查询模型,php,laravel,laravel-routing,Php,Laravel,Laravel Routing,我正在寻找平坦的电子商务商店搜索引擎优化的目的我的路线 我想创建以下路线: Route::get('/{country}', ['uses' => 'Store\ProductController@browseCountry']); Route::get('/{category}, ['uses' => 'Store\ProductController@browseCategory']') 国家和类别必须是动态的 我想知道这样的事情是否可行?以及实现这一目标的最佳方式 // Rou

我正在寻找平坦的电子商务商店搜索引擎优化的目的我的路线

我想创建以下路线:

Route::get('/{country}', ['uses' => 'Store\ProductController@browseCountry']);
Route::get('/{category}, ['uses' => 'Store\ProductController@browseCategory']')
国家
类别
必须是动态的

我想知道这样的事情是否可行?以及实现这一目标的最佳方式

// Route 1
Route::get('/{country}', ['uses' => 'Store\ProductController@browseCountry'])
    ->where('country', ProductCountry::select('slug')->get());

// Route 2
Route::get('/{category}', ['uses' => 'Store\ProductController@browseCategory'])
    ->where('category', ProductCategory::select('slug')->get());
示例路线:

/great-britain should be routed via Route 1
/china         should be routed via Route 1

/widgets       should fail route 1, but be routed via Route 2 because 
               widgets are not in the product_country table but are in 
               the product_category table
我知道我可以用可能的国家/地区对我的路线进行硬编码:

Route::get('/{country}', ['uses' => 'Store\ProductController@browse'])
    ->where('country', 'great-britain|china|japan|south-africa');

然而,这是笨拙和乏味的。我想从数据库中获取国家列表。

好的,在查看更新后的问题后,您需要在各自的模型中创建一个方法,将所有可用的slug与|字符连接起来,因此您可以调用如下内容:

Route::get('/{country}', ['uses' => 'Store\ProductController@browseCountry'])
     ->where('country', ProductCountry::getSlugs());
这将非常像你的例子中的“大不列颠、中国、日本、南非”,除非你不必写它


但是,我强烈建议您为路由提供更多的内容,/country/{country}或/category/{category}否则会造成混乱,URI结构通常是这样的,以便用户可以准确地看到它们所在的位置。

您需要路由过滤器来实现这一点

将以下代码放入
filters.php
route.php
文件中

Route::filter('country', function()
{

    $country = Country::where('slug', Route::input('country'))->first();
    if(  ! $country) {
        dd("We do not support this country");
            // Redirect::route('home');
    }

});
最后是你的路线:

Route::get('country/{country}', array('before' => 'country', 'uses' => 'Store\ProductController@browseCountry'));

我会这样做我选择国家/地区模型,因为模型较少+您需要缓存: 将列表(“名称”)更改为“国家/地区名称”列

Route::get('/{country}', ['uses' => 'Store\ProductController@browseCountry'])
->where('country', implode('|',ProductCountry::select('slug')->lists('name')));
它的作用是选择所有国家的名称并将它们作为数组返回,如下所示

 ('usa','england','thailand') 
并使用带“|”的内爆作为返回:

usa|england|thailand
所以你最后的路线是这样的:

Route::get('/{country}', ['uses' => 'Store\ProductController@browseCountry'])
->where('country', 'usa|england|thailand');

当然,这将在您的控制器中处理,而不是在您的路线中。您可以将country传递到控制器中,但如果country==do xxx,则控制器将执行您的操作,否则执行yyy等。您可以使用route::model()为了达到这个目的。@Dave-如果我想限制管制员可以使用特定的国家/地区,我该如何通过通往该国家/地区的路线?按照你的方法,我的路线必须是
route::get('country'/{country})
,这是我不想要的。@Anam-你能提供一个如何实现的例子吗?我已经更新了我的问题,以澄清我试图实现的目标。我要试一试。我理解你的建议,并在一定程度上同意你的建议。然而,你见过在URL中有“类别”或“国家”字样的在线商店吗?类别是的,国家不多。取决于目的really.country通常通过会话tbh存储在我看到的大多数商店中,这会中断我的迁移,因为artisan加载routes.php,如果不迁移,routes.php中的查询将失败。你有解决这个问题的办法吗?@Ezra为什么不把路由改成category/{category}之类的呢?我希望可以,但我需要构建的路由必须是这样工作的。我修复了它,允许我传递一个对Route:pattern和Route::where的闭包,并且只在laravel调用symfony路由器之前运行这个回调。Artisan实际上没有使用到该回调的路由,只有当该路由真正用于解决我的问题时,才会调用该回调!