Yii Framework Forum: Adding dash to actions. (e.g. http://yii.com/admin/add-account) - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Adding dash to actions. (e.g. http://yii.com/admin/add-account) Rate Topic: -----

#21 User is offline   badboyii 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 25-May 12

Posted 25 May 2012 - 03:20 AM

View Postenrico.detoma, on 18 January 2012 - 12:02 PM, said:

My solution (which currently seems to work) to map from /dash-controller/dash-action to /DashController/DashAction is:

class UrlManager extends CUrlManager {

    // remove dash to map dashed url with undashed controller/action
    public function parseUrl($request)
    {
        $url = parent::parseUrl($request);
        if (substr_count($url, "-") > 0) {
            
            $url = explode('-', $url);
            $url = array_map('strtolower', $url);
            $url = array_map('ucfirst', $url);
            $url = implode('',$url);
        }
        return $url;
    }
}


To avoid having to use parsePathInfo to restore dash for GET parameters injected from path, I changed my rules from:

...
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
...


to:

...
'<controller:[0-9a-zA-Z_\-]+>/<id:\d+>' => '<controller>/view',
'<controller:[0-9a-zA-Z_\-]+>/<action:[0-9a-zA-Z_\-]+>/<id:\d+>' => '<controller>/<action>',
'<controller:[0-9a-zA-Z_\-]+>/<action:[0-9a-zA-Z_\-]+>' => '<controller>/<action>',
...


I.e. I replaced the \w with a regular expression which includes the dash.


I apologize in advance if I am asking a newbie question, but I am just starting out with Yii.

I am trying to port a small, incomplete web site from Zend Framework to Yii. I followed the approach explained below to implement support for hyphens in controllers/actions and it seems to work just fine. The only problem I have is that I am not quite sure of where I should place that new class file in order to be autoloaded. I tried to create a folder called /UrlManager under /extensions, but it didn't work. Finally, I put it in the root of the application and it works... but is that the way to do it? I am assuming there must be a better approach.
0

#22 User is offline   g3ck0 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 96
  • Joined: 03-June 09

Posted 14 June 2012 - 08:14 AM

You should create a file under protected/components called e.g. CMyUrlManager. Then you have to add this component to your configuration file. In general this is the main.php under protected/config. There is a components-section where you can find a urlManager entry. There you can add a key called class and as value you have to set the name of your UrlManager class.

Mine looks like this:
return array(
	// application components
	'components' => array(
		'user' => array(
			//...
		),
		// uncomment the following to enable URLs in path-format
		'urlManager' => array(
			'class' => 'CApUrlManager', // < Here you have add your class name!
			'urlFormat' => 'path',
			'showScriptName' => false,
			'rules' => array(
				'<controller:\w+>/<id:\d+>' => '<controller>/view',
				'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
				'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
			),
		),
	),
);

0

#23 User is offline   psolovyov 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 22-March 12

Posted 19 June 2012 - 12:35 PM

Hey guys I tried adding url rules to my Url Manager:

'<controller:[\w\-]+>/<action:[\w\-]+>/<id:\d+>'=>'<controller>/<action>',
'<controller:[\w\-]+>/<action:[\w\-]+>'=>'<controller>/<action>',	


or

'<controller:[a-zA-Z_\-]+>/<action:[a-zA-Z_\-]+>/<id:\d+>'=>'<controller:[a-zA-Z_\-]+>/<action>',
'<controller::[a-zA-Z_\-]+>/<action:[a-zA-Z_\-]+>'=>'<controller:[a-zA-Z_\-]+>/<action>',


I still get 404.

I think I'm doing something wrong with action names inside controller or names of view files.

my view file:

client-update.php

my action name inside Controller

"actionClientUpdate()"

Where am I going wrong with this?
0

#24 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,035
  • Joined: 30-October 09
  • Location:UK

Posted 04 August 2012 - 05:45 PM

Does anyone know if this functionality has been added to Yii?
0

#25 User is offline   nsanden 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 06-December 11

Posted 19 October 2012 - 03:53 PM

View Postenrico.detoma, on 18 January 2012 - 12:02 PM, said:

My solution (which currently seems to work) to map from /dash-controller/dash-action to /DashController/DashAction is:

class UrlManager extends CUrlManager {

    // remove dash to map dashed url with undashed controller/action
    public function parseUrl($request)
    {
        $url = parent::parseUrl($request);
        if (substr_count($url, "-") > 0) {
            
            $url = explode('-', $url);
            $url = array_map('strtolower', $url);
            $url = array_map('ucfirst', $url);
            $url = implode('',$url);
        }
        return $url;
    }
}


To avoid having to use parsePathInfo to restore dash for GET parameters injected from path, I changed my rules from:

...
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
...


to:

...
'<controller:[0-9a-zA-Z_\-]+>/<id:\d+>' => '<controller>/view',
'<controller:[0-9a-zA-Z_\-]+>/<action:[0-9a-zA-Z_\-]+>/<id:\d+>' => '<controller>/<action>',
'<controller:[0-9a-zA-Z_\-]+>/<action:[0-9a-zA-Z_\-]+>' => '<controller>/<action>',
...


I.e. I replaced the \w with a regular expression which includes the dash.


Just want to note, using the parseUrl function above often breaks cgridview paging links because they contain dashes (ie, .../ajax/my-cgridview-div-id/..)
0

#26 User is offline   sitamet 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 08-October 12

Posted 14 January 2013 - 04:02 AM

Looking for a "route dashed url" implementattion I've also found this related yii wiki article:
http://www.yiiframew...url-management/
0

#27 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,035
  • Joined: 30-October 09
  • Location:UK

Posted 26 June 2013 - 03:28 AM

How come we can't just create an action with underscore, for example:

public function actionEdit_Account()
{

}


And accessing URL:
/account/edit-account/
should automatically route to this action?
0

#28 User is offline   jayasam 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 07-April 13

Posted 12 January 2014 - 01:59 AM

View Posttwisted1919, on 10 January 2011 - 03:28 PM, said:

In order to have this kind of functionality working i have something like :

class Xyx extends CController{

    // this is what does all the hard work
    public function missingAction($action)
    {
        $action=str_replace('-','_',$action);
        $action='action'.ucfirst(strtolower($action));
        if(method_exists($this,$action))
            $this->$action();
        else
            $this->actionIndex();
    }

    public function actionMy_fancy_action_here()
    {
       //the url can be 
       // A) : /xyz/my-fancy-action-here
       // B) : /xyz/my_fancy_action_here
    }


}


Pretty neat ha ? :)


I have a URL like this
www.mydomain.com/post/update-post/3


I cant pass id to my action method
actionUpdatePost($id)


How to pass id from missingAction to update method
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • 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