Yii Framework Forum: how to config urlManager rules with aliases and $_GET parameter - Yii Framework Forum

Jump to content

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

how to config urlManager rules with aliases and $_GET parameter

#1 User is offline   Maik 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 05-December 14

Posted 05 December 2014 - 08:34 AM

Hi,
for my current (advanced) project i just need one controller (SiteController). So there is no need to show it in the url. Thats why i added this rule to the frontend config:

'urlManager' => [
    'rules' => array(
        '<alias:product|contact|about>' => 'site/<alias>',
    ),


This is working fine and localhost/product points to localhost/site/product.

Of course, i activated prettyUrl and added the this default rules to the common config:

 'rules' => array(
                '<controller:\w+>/<id:\w+>' => '<controller>',
                '<controller:\w+>/<action:\w+>/<id:\w+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            ),


Now i want to access a GET parameter like this: localhost/product/productname. But i get the error:

Quote

Unable to resolve the request "product"


but localhost/site/product/productname is working properly... The "productname" should be $_GET['id']. What do i have to change to make this happen?

Thanks!
0

#2 User is offline   hrnair 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 09-December 12

Posted 05 December 2014 - 08:49 AM

A dirty solution:

if you have only few products, make a default product controller and product names as actions.

Sorry for a real answer as my solutions for urlmanagement are not working in yii2 advanced.
0

#3 User is offline   Maik 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 05-December 14

Posted 05 December 2014 - 11:41 AM

I got the solution via stackoverflow.com.
i had to put all the common rules into the frontend rules and add a second alias rule:
'rules' => array(
    '<alias:contact|about>' => 'site/<alias>',
    '<alias:product>/<id:\w+>' => 'site/<alias>',
    '<controller:\w+>/<id:\w+>' => '<controller>',
    '<controller:\w+>/<action:\w+>/<id:\w+>' => '<controller>/<action>',
    '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),

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