[英]How to make multi-level url structure in Laravel 4?
I am looking to create a URL structure like site.com/mycat/myorg/myexam/mysyllabus and site.com/mycat/myorg/myexam and so on for all params. 我正在寻找为所有参数创建类似site.com/mycat/myorg/myexam/mysyllabus和site.com/mycat/myorg/myexam之类的URL结构。
Currently I'm passing the params from URL to the function like: 目前,我正在将参数从URL传递给以下函数:
Route::get('{cat}/{org}/{exam}/{syllabus}', 'homeController@myPage');
and my function looks like this: 我的函数看起来像这样:
public function myPage($cat, $org, $exam, $syllabus) {
$result = myModel::where('syllabus_slug', $syllabus)->first();
if(empty($result)) { App::abort(404); }
else
return View::make('myPage', $result);
}
I think it would be very resource intensive to check for all url params at every request from the database. 我认为在数据库的每个请求中检查所有url参数将非常耗费资源。 Currently i'm only checking the $syllabus param from database but this way any value of $org, $cat can be passed without any error.
目前,我仅从数据库中检查$ syllabus参数,但是这种方式可以传递$ org,$ cat的任何值而不会出现任何错误。 Ofcourse I can check for all params from database but i think that's not the right way to work with URLs with this awesome framework.
当然,我可以检查数据库中的所有参数,但是我认为这不是在这种出色的框架上使用URL的正确方法。
Update: I also want to make cat and org optional. 更新:我也想使cat和org为可选。
Do you only have a set amount of categories, organizations, etc? 您是否只有一定数量的类别,组织等? If so, Laravel comes with something called Route Prefixing , where you can do something like this for each of the categories and organizations:
如果是这样,Laravel附带了一个称为Route Prefixing的东西,您可以在其中为每个类别和组织执行以下操作:
Route::group(array('prefix' => 'myCategory'), function()
{
Route::group(array('prefix' => 'myOrg'), function()
{
Route::group(array('prefix' => 'myExam'), function()
{
Route::get('/', 'homeController@myPage');
});
Route::group(array('prefix' => 'myOtherExam'), function()
{
Route::get('/', 'homeController@myOtherPage');
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.