Yii Framework Forum: Support actions with dash - Yii Framework Forum

Jump to content

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

Support actions with dash SEO optimized URLs

#1 User is offline   lubosdz 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 114
  • Joined: 25-July 10
  • Location:Slovakia, Bratislava

Posted 08 October 2011 - 11:51 AM

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.yiiframew...post__p__101686

Thanx
Lubos
Yii extension: Captcha Extended

Greatest discoveries in 22nd century will be about the gravitation. | http://www.synet.sk | http://ipdf.sk
2

#2 User is offline   schmunk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 566
  • Joined: 02-November 08
  • Location:Stuttgart, Germany

Posted 19 October 2011 - 06:33 AM

I am not quite sure about this, but couldn't this be done with a custom url manager in Yii 1.x?
Phundament - Yii Application Boilerplate with composer support
Fork on github

Follow phundament on Twitter

DevSystem: Mac OS X 10.7 - PHP 5.3 - Apache2 - Yii 1.1 / trunk - Firefox or Safari
0

#3 User is offline   outrage 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 313
  • Joined: 10-November 09
  • Location:Blackpool, United Kingdom

Posted 02 November 2011 - 07:12 PM

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.
0

#4 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 03 November 2011 - 05:40 AM

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.
0

#5 User is offline   alex-w 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 222
  • Joined: 25-November 10

Posted 03 November 2011 - 07:03 AM

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

This post has been edited by alex-w: 17 November 2011 - 03:46 AM

4

#6 User is offline   dyulax 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 26-May 11
  • Location:Brazil

Posted 16 November 2011 - 05:07 PM

Actions with dashes are far more SEO friendly and readable than "somethingLikeThis".
Do what thou wilt shall be the whole of the Law.
1

#7 User is offline   Jaggi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 05-September 11

Posted 21 November 2011 - 06:28 PM

View Postdyulax, on 16 November 2011 - 05:07 PM, said:

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.
See my development site @ www.CodeTheInter.net (BETA)

Posted Image Posted Image

Quote

If you make it idiot proof, they'll build a better idiot
0

#8 User is offline   schmunk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 566
  • Joined: 02-November 08
  • Location:Stuttgart, Germany

Posted 21 November 2011 - 08:22 PM

I always thought underscores would be the best option?

/something_like_this/my_thing

Phundament - Yii Application Boilerplate with composer support
Fork on github

Follow phundament on Twitter

DevSystem: Mac OS X 10.7 - PHP 5.3 - Apache2 - Yii 1.1 / trunk - Firefox or Safari
0

#9 User is offline   Ben 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 266
  • Joined: 15-March 09

Posted 22 November 2011 - 08:00 AM

Joomla, wordpress and drupal use dashes.

Random samples:
http://www.joomla.or...-cms-award.html
http://lynchd.wordpr...dventure-day-3/
http://drupal.org/best-practices
Don't like ads in my sig...
0

#10 User is offline   Jaggi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 05-September 11

Posted 22 November 2011 - 09:22 AM

View PostBen, on 22 November 2011 - 08:00 AM, said:



As does Zend.
See my development site @ www.CodeTheInter.net (BETA)

Posted Image Posted Image

Quote

If you make it idiot proof, they'll build a better idiot
0

#11 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 22 November 2011 - 11:52 AM

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
"Less noise - more signal"
1

#12 User is offline   ekerazha 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 525
  • Joined: 10-October 08
  • Location:European Union

Posted 07 December 2011 - 01:14 PM

View PostBen, on 22 November 2011 - 08:00 AM, said:



Wikipedia uses underscores :lol:

However, +1 for dashes over underscores.
Yii user #37
0

#13 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 07 December 2011 - 01:17 PM

View PostBen, on 22 November 2011 - 08:00 AM, said:

Joomla, wordpress and drupal use dashes.

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.
"Less noise - more signal"
0

#14 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,448
  • Joined: 04-October 10

Posted 07 December 2011 - 02:29 PM

View Postjacmoe, on 22 November 2011 - 11:52 AM, said:

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


Great resource Jacmoe!
┬┐How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#15 User is offline   Jaggi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 05-September 11

Posted 07 December 2011 - 05:50 PM

View Postjacmoe, on 07 December 2011 - 01:17 PM, said:

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.


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
See my development site @ www.CodeTheInter.net (BETA)

Posted Image Posted Image

Quote

If you make it idiot proof, they'll build a better idiot
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