我想问一下,是否有可能在Laravel中得到像动态路线这样的东西。 我有一个基于模块的个人CMS。每个模块==控制器。在数据库中,我有一个称为“结构”的表,该表存储页面的所有子站点。项目的名称应为任意,因此我不知道该项目的名称,因为它基于用户的意愿。因此,文章模块应命名为“新闻”,“文章”,“博客”等。我不知道。这意味着,我无法将路线设置为确切的单词,因为它的名称可能是任何东西。这就是为什么我将这些数据存储到数据库中的原因。在数据库中,我还有module_name,它将指向控制器。
因此,如果用户设置例如: 博客->模块文章, 我需要像这样从db获取路由:
Route::get('/Blog/', 'ArticleController@index');
“博客”和“文章”都存储在DB中。
我的问题是,我无法从数据库检索此数据。从“最佳实践”的角度来看,我该怎么做? 我试图写
use DB;
在routes / web.php的顶部,但是它不起作用。我也认为,这是代码镇流器。
如果有人可以帮助我,我将非常感激。谢谢
答案 0 :(得分:1)
实际上不知道为什么要这么做
你的意思是这样吗
// route
Route::get('/{slug}', 'RedirectController@redirect');
// RedirectController
public function redirect($slug)
{
// in your example slug is the column name of the 'Blog'
$to = \DB::table('route_table')->where('slug', $slug)->firstOrFail();
// module is the column name of Article
return redirect()->action($to->module . 'Controller@index');
}
答案 1 :(得分:0)
这样路由,设置通配符
Route::get('/{name}', 'ArticleController@show');
然后在您的控制器中,使用index()方法:
public function show($name)
{
//$name now is equal to whatever the user put into the route
//do some database stuff with $name to fetch the proper data
}