我想问一下,在Laravel中是否有可能获得像动态路由这样的东西。我有一个个人CMS,基于模块。每个模块的==控制器。在数据库中,我有一个名为“结构”的表,其中存储了页面的所有子站点。item的 name 应该是any,所以我不知道item的名称,因为它基于用户的意愿。所以文章模块应该被命名为“新闻”,“文章”,“博客”等等。我不知道。这意味着,我不能将路由设置为确切的单词,因为它的名称可以是任何名称。这就是我将这些数据存储到数据库的原因。在数据库中,我还有module_name,它将指向控制器。
因此,如果用户设置例如:博客->模块文章,我需要从数据库获取路由,如下所示:
Route::get('/Blog/', 'ArticleController@index');
“博客”和“文章”都存储在数据库中。
我的问题是,我无法从数据库中检索这些数据。从“最佳实践”的角度来看,我应该如何做到这一点?我试着写
use DB;
在routes/web.php的顶部,但它不起作用。我也认为,这是代码镇流器。
如果有人能帮我这个忙,我将不胜感激。谢谢
发布于 2018-07-12 05:12:35
其实我不知道你为什么要这么做
你的意思是这样的吗?
// 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');
}
发布于 2018-07-12 04:18:29
像这样的路由,设置一个通配符
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
}
https://stackoverflow.com/questions/51293440
复制相似问题