Support the ongoing development of Laravel.io →
posted 9 years ago
Configuration
Last updated 2 years ago.
0

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.

0

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:

  1. domain.com/post/2015/03/first-post
  2. domain.com/post/2015/03/31/first-post
  3. domain.com/post/first-post
  4. domain.com/post?id=1

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.

Last updated 9 years ago.
0

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
    }

});
Last updated 9 years ago.
0

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.

Last updated 9 years ago.
0

@astroanu

and if someone makes some crazy url with 18 segments?

0

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.

Last updated 9 years ago.
0

https://github.com/octoberrain/cms/blob/master/routes.php

Basicaly what I said. Custom functionality behind any route.

@astroanu

Yes it depends on needs.

Last updated 9 years ago.
0

What is your requirement for doing :

mydomain.com/post?id=1

over :

mydomain.com/post/{id}

(just curious)

0

Hi, Thanks for all your suggestions, I will try it and update here.

@RemiCollin

Oh nothing.. that was just for example, I just want to describe that I need to create customization like Wordpress do :D

0

Sign in to participate in this thread!

Eventy

Your banner here too?

liwewe liwewe Joined 1 Apr 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.