流明.更改网址 [英] Lumen. Change url
问题描述
我正在一个在Lumen框架中构建的网站上工作.我的问题是路由.它现在可以正常工作,但是我希望这些网址看起来更好.
Im working on a site wich i building in the Lumen framework. My problem is the routing. It works fin as it is now, but i want the urls to look better.
代码:
routes.php
routes.php
$app->get('/work/{id}', 'WorkController@getWork');
WorkController.php
WorkController.php
public function getWork($id) {
$project = DB::select("SELECT * FROM projects where id = $id");
return view('work.index')->with('project', $project);
}
这是一个类似投资组合的网站,我想通过它的ID获得特定的数据库帖子.但我希望网址看起来更好.现在,这些网址看起来像: www.site.com/work/2
其中2是ID.例如,我如何才能使用db-posts标题字段作为url?
Its a portfolio-like site where i want to get a specifik db-post by its id.
But i want the urls to look better. Now the urls looks like: www.site.com/work/2
where 2 is the id. How can i instead for example use the db-posts title field as the url?
推荐答案
您可以在项目表的每一行中存储一个urltitle列.这将保存项目的字符串标识符.例如,您可以将项目标题转换为小写,并用破折号替换每个空格.(您还必须处理特殊字符)
You could store a urltitle column in each row of the project table. This would hold a String identifier of the project. For example you could convert the project title to lower case and replace each space with a dash. (You would also have to deal with special characters)
您的路线如下所示:
$app->get('/work/{urltitle}', 'WorkController@getWork');
您的查询将如下所示:
public function getWork($urltitle) {
$project = DB::select("SELECT * FROM projects where urltitle = $urltitle");
return view('work.index')->with('project', $project);
}
新网址的示例为 www.mysite.com/work/my-first-project
.
希望有帮助.
这篇关于流明.更改网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!