Support actions with dash

Hi,

please can you add build-in support for controller actions having dash in the action name?

Then




http://www.mysite.com/cms/products-and-services



would be routed to method "actionProductsAndServices" inside of "CmsController".

Here is a workaround applicable to Yii 1.X.X, that unfortunatelly will not highlight CMenu active item:




/**

* Catch all action - used as a workaround to support dashed action names like my-action => actionMyAction (compatible with Zend Framework)

* @param string $action Action that was not found within this controller

*/

public function missingAction($action){

   $action = explode('-', $action);

   $action = array_map('strtolower', $action);

   $action = array_map('ucfirst', $action);

   $action = implode('',$action);

   if(method_exists($this,'action'.$action) || array_key_exists('action'.$action, $this->actions())){

      $this->setAction($action);

      $this->run($action); 

   }else{

      throw new CHttpException(404, Yii::t('main','Action "{action}" does not exist in "{controller}".', array(

         '{action}' => 'action'.$action,

         '{controller}' => get_class($this),

      )));

   }

}



Actually if possible, I would love to see this support also for controller names, like




http://www.mysite.com/mobile-technology/products-and-services



would be routed to method "actionProductsAndServices" inside of "MobileTechnologyController".

Algorithm for stripping off dashes "-" is not important, important is to have built-in support for this feature.

See also discussion on the problem:

http://www.yiiframework.com/forum/index.php?/topic/14990-adding-dash-to-actions-eg-httpyiicomadminadd-account/page__view__findpost__p__101686

Thanx

Lubos

I am not quite sure about this, but couldn’t this be done with a custom url manager in Yii 1.x?

It seems that this wouldn’t cause any issues.

A framework’s job is to make things easier for the user.

Why make the user create a custom url manager for a common scenario?

+1

Edit:

Actually I just read in another post that Yii 2 will have backwards compatibility?

If that is the case, this could break existing url manager rules.

Sorry, but I’m sure I’ve read previously that Yii 2 would not be backwards compatible. My bad, I read too much :)

Of course, the core could check the existing url manager rules to see if there is a conflict, or simply make it an option.

Just define those rules manually?


'rules' => array(

   'cms/products-and-services' => 'cms/productsAndService',

),

That’s what this feature is there for.

Adding more functionality/magic doesn’t sound good. It’s not like “strip those dashes” would be the only case to cover? Maybe for your application, but not for others. So I suggest to define those rules manually or alternatively use a custom url rule class that does the stripping.

Could do something like this,




'urlManager' => array(

    'urlRuleClass' => 'ERuleTest',

    ...

)






class ERuleTest extends CUrlRule

{


    public function parseUrl($manager, $request, $pathInfo, $rawPathInfo) {

        $pathInfo = preg_replace_callback('/[-]([a-z])/i', function($matches) {

            return strtoupper($matches[1]);

        }, $pathInfo);

        return parent::parseUrl($manager, $request, $pathInfo, $rawPathInfo);

    }

    

}



Should’ve said, This will make all Url rules extend from my class.

It will change, some-action-here to someActionHere

Actions with dashes are far more SEO friendly and readable than "somethingLikeThis".

Agree with this, when creating a site we shouldn’t have to write out 100’s of url rules either so thats not a solution. This can be implemented easily enough with backward compatibility. Also according to most SEO standards dashes are the accepted method and the most used method around the internet.

I always thought underscores would be the best option?


/something_like_this/my_thing

Joomla, wordpress and drupal use dashes.

Random samples:

As does Zend.

Everybody does.

About underscores in URLs: it’s a bad idea.

By default, links are displayed underlined, making it impossible to see if the url has spaces or underscores.

Also see this on YouTube:Underscores vs. dashes in URLs

Wikipedia uses underscores :lol:

However, +1 for dashes over underscores.

As does everyone else.

I think you are confusing slugs with actions.

You use a dash instead of a space. That’s basically what they do.

MediaWiki replaces spaces with underscores.

That’s not really the same thing as what’s requested here.

Great resource Jacmoe!

MediaWiki is a old buggy outdated piece of software though so not really a benchmark.

I don’t think underscore is really part of the argument as Yii currently uses camel case so its more camel case vs dashes.

+1 dashes