Yii Framework Forum: Prettyurl Route Params - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Prettyurl Route Params

#1 User is offline   mfrancis107 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 19-July 12

Posted 08 October 2013 - 12:13 PM

In Yii one we could do something like

public class SiteController {

public function actionUser($id)


and then with prettyUrls enabled use a url like /site/user/id/1

But in Yii2 it doesn't seem to work like that. I can only get it to work like /site/user?id=1

Is this a design decision or am I setting things up incorrectly. I tend to prefer the second url, but it's not what I was expecting.
0

#2 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 532
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 08 October 2013 - 04:56 PM

The first form should work too but it depends on your config. How did you configure UrlManager, which rules did you add?
0

#3 User is offline   mfrancis107 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 19-July 12

Posted 18 November 2013 - 11:53 AM

View PostCeBe, on 08 October 2013 - 04:56 PM, said:

The first form should work too but it depends on your config. How did you configure UrlManager, which rules did you add?


Here's my urlManager setup

'urlManager'=>array(
            'enablePrettyUrl'=>true,
            'showScriptName'=>false,
)


In the documentation it says the following should create /site/page/id/about but it creates /site/page?id=about
echo \Yii::$app->urlManager->createUrl('site/page', ['id' => 'about']);

0

#4 User is offline   Tropi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 30-November 11

Posted 18 November 2013 - 07:27 PM

By the way, will there be a way to define named routes? Or do I always have to know the Controller, the action + the parameters? Compare http://four.laravel....ng#named-routes
0

#5 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,735
  • Joined: 17-January 09
  • Location:Russia

Posted 19 November 2013 - 04:15 AM

No named routes. I see no reason to add another layer. Will add more confusion.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#6 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 532
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 20 November 2013 - 07:30 AM

@mfrancis107: you have to define a rule that allows parameter to be part of the url path:

    'site/user/id/<id:\d+>' => 'site/user',


@Tropi: I do not see the benefit about giving a name/alias to a route.
When you want to redirect somewhere you say 'controller/action' and url will be build from urlmanager rules. This concept is not needed as we already have a fixed schema for routing in yii. Laravel does not have this as far as I see from the docs there.
0

#7 User is offline   Tropi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 30-November 11

Posted 20 November 2013 - 09:33 AM

Let's take your example from above:
    'site/user/id/<id:\d+>' => 'site/user',


So link to user "tropi" would be: site/user/54083. In code this is:
echo \Yii::$app->urlManager->createUrl('site/user', ['id' => '54803']);


So what if I want to do it like in the forums? Profile URIs here look like: site/user/54083-tropi. If I want to include the username in links (for example for SEO), I have to edit ALL the createUrl() statements instead of just changig one named "profile route".

But I see, it's not going to come.
0

#8 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 532
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 20 November 2013 - 09:41 AM

View PostTropi, on 20 November 2013 - 09:33 AM, said:

So link to user "tropi" would be: site/user/54083. In code this is:


Not 100% correct, it would be site/user/id/54083

Quote

So what if I want to do it like in the forums? Profile URIs here look like: site/user/54083-tropi. If I want to include the username in links (for example for SEO), I have to edit ALL the createUrl() statements instead of just changig one named "profile route".


How would the named "profile route" know the name when in createUrl() it only gets an ID?
You can do this by creating your own UrlRule class that determines users name but I doupt that you want an SQL query each time you create an url.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users