Yii Framework Forum: Two problems with rules in pretty urls - Yii Framework Forum

Jump to content

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

Two problems with rules in pretty urls

#1 User is offline   andronic 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 28-July 15

Posted 08 November 2017 - 05:56 AM

1)

I used rules from UrlManager like this:

      'home'         =>   'site/index',
      '<alias:\w+'   =>   'site<alias>', 


And it works I can access my_domain.com/home my_domain.com/contact etc.
Without the first rule I cannot access my_domain.com/home, I would like to have one generic rule not two...so I can resolve the next problem.

2)

After I set up those 2 rules...I installed and configured this language picker http://www.yiiframew...anguage-picker/

Now I have two flag-links with
my_domain.com/home?language-picker-language=ro-RO
my_domain.com/contact?language-picker-language=en-GB
....

Well I'm a beginner and I don't know how to create these rules so I can have
my_domain.com/ro/home
my_domain.com/en/home
....
0

#2 User is offline   skworden 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 525
  • Joined: 14-June 12
  • Location:Virginia, USA

Posted 08 November 2017 - 11:55 PM

You will most likely have to extend url manager to be able to use the urls like you are wanting. You will end up having to make a bunch of rules which will slow your site down if you don't do something globally like extending url manager. There are a few extensions that you can use here is one but there are more if you search github

https://github.com/c...yii2-localeurls


here is an old forum post too

http://www.yiiframew...site-url-rules/

yii2 url routing guide

http://www.yiiframew...me-routing.html

0

#3 User is offline   andronic 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 28-July 15

Posted 10 November 2017 - 02:12 AM

Ok, I'm thinking to use this extension, but I don't understand a thing... it creates urls(the same way like in the routing guide) like this

     $url = Url::to(['demo/action']);


And I do not know WHERE to create this rules. Would the creation is made in the controller?
0

#4 User is offline   skworden 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 525
  • Joined: 14-June 12
  • Location:Virginia, USA

Posted 11 November 2017 - 09:44 AM

You would set the rules in your main config file under components. You would also set the rules like you normally would.
0

#5 User is offline   alrazi 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,537
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 14 November 2017 - 12:24 AM

perhaps the guides are not very clear, but you confusing it with something which is related to routing but has different purpose

// imagine you have defined a set of routes
'home'         =>   'site/index',
'<alias:\w+'   =>   'site<alias>', 


you might wanna generate links to these routes to go from one page to another page
in your case lets say you wanna put a link on a page which takes you to site/index action or home route you can use Url::to

// this will generate a link to your homepage
<a href="<?= Url::to(['site/index']) ?>Go to homepage</a>

0

#6 User is offline   andronic 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 28-July 15

Posted 14 February 2018 - 06:54 AM

Ok I suspend the problem for a few months. But now I'll try to fix it.


Some corrections to my first post
 'home'   =>   'site/index',
 '<alias:\w+>'   =>   'site/<alias>', 


I used https://github.com/c...yii2-localeurls

Now I have

     '<alias:\w+(-\w+)*>'   =>   'site/<alias>',





And it works, my default language in my config is ro...and I want that my app to ignore THE DEFAULT LANGUAGE OF THE BROWSER... or any cookies/sessions related to language...so that de default language will be always ro.


LE:
For sessions/cookies I found this solutions

    'enableLanguagePersistence' => false,
    'languageSessionKey'        => false,
    'languageCookieDuration'    => false,

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