Laravel通过参数路由到控制器
I'm trying to have clean URLs with out the ID displayed. For example, I currently have:
/Article/2
/Article/3
...
Instead I want those urls to switch to something like this:
/Article/2/ -> /My_Super_Awesome_Article/
/Article/3/ -> /Another_Cool_Article
My route looks like this:
Route::resource('/article', 'ArticleController');
And the controller like this:
class MenuController extends \BaseController {
public function show($id)
{
$page = Model::FindOrFail($id);
return View::make('article.show')->withPage($page);
}
// more functions
===================================================
WHAT WORKS BUT I THINK IS A BAD WAY OF DOING IT:
I figure out a way to do this... but this is for sure not the best approach. This is the technique I'm currently using to substitute:
In the controller, I add this
public function Clean_Url_20()
{
$page = Model::FindOrFail( 20 );
return View::make('article.show')->withPage($page);
}
And in the routes, I use this:
Route::get('/My_Super_Cool_URL', 'ArticleController@Clean_Url_20');
So... Is there a better way to achieve this?
You lack flexibility if you give every single article a route. You have to define 1000 routes if you have 1000 articles.
Why not define these routes in another way? For example:
Route::get('article/{articleSlug}','ArticlesController@show');
In your controller you just use the slug to find a particular article.
By the way, you can use Str::slug() to format your article titles when you are preparing your article data.
Route::get('/My_Super_Awesome_Article/{id}', array('as' => 'my.article.show', 'uses' => 'ArticleController@show'));
your route was wrong. give an attention to the {id}