Obviously you need to save the unique slug name on to your model. Using the routes you can pass the slug name like this:
Route::get('post/{slug}', function($slug)
{
return Post::where('slug', '=', $slug);
});
if you need to use both id and slug all you need to do it check if the slug is a int or not.
Hi, thanks for your answer. I believe your solution should be working on my sample problem before.
But what I really need is more complicated than that, I need to create "permalink settings" exactly like Wordpress do. I should have some default permalink, for example:
and beside that, user can also setting custom URL structure, for example:
5. domain.dom/%year%/%monthnum%/%postname%
How can I accomodate that kind of requirement using laravel?
Can I just pass variable to routes.php like this ?
$customURL = "/{year}/{monthnum}/{postname}";
Route::get($customURL, function($year, $monthnum, $postname)
{
//do something
});
Any suggestion on this would be greatly appreciated. Thanks.
This is do-able.. The following is untested code. This was my first thought on this but there maybe other more simplified ways to do this. Of course it need more validation than just checking is_null
Route::get('post/{p1?}/{p2?}/{p3?}', function($p1 = null, $p2 = null, $p3 = null)
{
// now you need to determine the slug format based on the params
switch (true) {
case (!is_null($p1) && !is_null($p2) && !is_null($p3)):
// we have 1,2 and 3; get by year, month and slug name
break;
case (!is_null($p1) && !is_null($p2)):
// we have 1 and 2; get by year and slug name
break;
case (!is_null($p1)):
// we have 1; get by slug name
break;
case (Input::has('id')):
// we have and id; get by id
break;
default:
// 404
}
});
You can't do that. If you need this all custom functionality, then use 'route any' with one parameter.
http://laravel.com/docs/5.0/routing#basic-routing
After setting 'route any', you can parse segments and 'get' parameters and make custom functionality on top of it.
You must check out how October cms is doing this.
@sukonovs using any will make things more complex. I guess nobody will want 18 segments in a url.
https://github.com/octoberrain/cms/blob/master/routes.php
Basicaly what I said. Custom functionality behind any route.
Yes it depends on needs.
What is your requirement for doing :
mydomain.com/post?id=1
over :
mydomain.com/post/{id}
(just curious)
Hi, Thanks for all your suggestions, I will try it and update here.
Oh nothing.. that was just for example, I just want to describe that I need to create customization like Wordpress do :D
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community