官术网_书友最值得收藏!

Using regular expressions in URL rules

One of the "hidden" features of Yii URL router is that you can use regular expressions that are pretty powerful when it comes to strings handling.

Getting ready

  1. Create a fresh Yii application using yiic webapp as described in the official guide and find your protected/config/main.php. It should contain the following:
    // application components
    'components'=>array(
       …
       // uncomment the following to enable URLs in path-format
       /*
       'urlManager'=>array(
          'urlFormat'=>'path',
          'rules'=>array(
             '<controller:\w+>/<id:\d+>'=>'<controller>/view',
             '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
             '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
          ),
       ),
  2. Delete everything from rules as we are going to start from scratch.
  3. In your protected/controllers, create PostController.php with the following code inside:
    class PostController extends CController
    {
      public function actionView($alias)
      {
          echo "Showing post with alias $alias.";
      }
    
      public function actionIndex($order = 'DESC')
      {
          echo "Showing posts ordered $order.";
      }
    
      public function actionHello($name)
      {
       echo "Hello, $name!";
      }
    }

    This is our application controller we are going to access using our custom URLs.

  4. Configure your application server to use clean URLs. If you are using Apache with mod_rewrite and AllowOverride turned on, then you should add the following lines to the .htaccess file under your webroot folder:
    Options +FollowSymLinks
    IndexIgnore */*
    RewriteEngine on
    
    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # otherwise forward it to index.php
    RewriteRule . index.php

How to do it...

We want our PostController actions to accept parameters according to some rules and give "404 not found" HTTP response for all parameters that do not match. In addition, post index should have an alias URL archive.

Let's use regular expressions to achieve it:

'post/<alias:[-a-z]+>' => 'post/view',
'(posts|archive)' => 'post/index',
'(posts|archive)/<order:(DESC|ASC)>' => 'post/index',
'sayhello/<name>' => 'post/hello',

Now, you can try the following URLs:

// success

http://example.com/post/test-post

// fail

http://example.com/post/another_post

// success

http://example.com/posts

// success

http://example.com/archive

// fail

http://example.com/archive/test

// success

http://example.com/posts/ASC

// success

The following screenshot shows that the URL http://example.com/post/test-post has run successfully:

The following screenshot shows that the URL http://example.com/archive/test did not run successfully and encountered an error:

How it works...

You can use regular expressions in both parameter definition and the rest of the rule. Let's read our rules one by one.

'post/<alias:[-a-z]+>' => 'post/view',

Alias parameter should contain one or more English letter or a dash. No other symbols are allowed.

'(posts|archive)' => 'post/index',

Both posts and archive are leading to post/index.

'(posts|archive)/<order:(DESC|ASC)>' => 'post/index',

Both posts and archive are leading to post/index. Order parameter can only accept two values: DESC and ASC.

'sayhello/<name>' => 'post/hello',

You should specify the name part but there are no restrictions on what characters are allowed.

Note that regardless of the rule used, the developer should never assume that input data is safe.

There's more...

To learn more about regular expressions, you can use the following sources:

See also

  • The recipe named Configuring URL rules in this chapter
  • The recipe named Creating URL rules for static pages in this chapter
主站蜘蛛池模板: 焦作市| 登封市| 天全县| 晋中市| 丹江口市| 广东省| 鸡泽县| 北辰区| 邵武市| 焉耆| 广汉市| 禹州市| 无棣县| 镇坪县| 邵阳县| 霞浦县| 繁昌县| 吴江市| 太谷县| 丽江市| 马鞍山市| 土默特右旗| 南雄市| 东海县| 南开区| 太康县| 稷山县| 丹东市| 保亭| 凯里市| 辽宁省| 崇仁县| 永安市| 安丘市| 寿阳县| 清涧县| 营山县| 甘孜县| 盈江县| 梓潼县| 芮城县|