Yii advanced url rewrite

前端 未结 3 1103
Happy的楠姐
Happy的楠姐 2021-02-02 03:01

pay attention to that ANYTHING_ELSE So, I have my controllers and actions that I want to behave as normal in response to examples like this:

// for UserContoller         


        
3条回答
  •  被撕碎了的回忆
    2021-02-02 03:26

    I shall explain step by step how to get this working.

    Step 1 - Create an Yii web app

    Navigate to your Yii framework path in your console and create a new webapp. In my case I used this in my console:

    cd c:\zeus\yii-1.1.10.r3566\framework
    yiic webapp c:\zeus\www\yiiblog
    

    where c:\zeus\yii-1.1.10.r3566\framework is my path to Yii php framework and c:\zeus\www\yiiblog is the path to my Yii webapp test folder

    Stept 2 - fake my domain to dev.yiiblog.com

    Go to C:\Windows\System32\drivers\etc and edit your hosts file by adding this line:

    127.0.0.1 dev.yiiblog.com
    

    Step 3 - alter apache httpd.conf file

    
        DocumentRoot "c:/zeus/www/yiiblog"
        ServerName dev.yiiblog.com
        ErrorLog "logs/dev.yiiblog.com-error.log"
        CustomLog "logs/dev.yiiblog.com-access.log" common
    
    

    and restart apache service. I used in my windows console:

    net stop apache
    net start apache
    

    where my Apache 2 service is named "apache" not "apache2.2" like the default one.

    Step 4 - create a database and configure a database connection into Yii

    I've created a database yiitest and a user yiitest. Then I opened my Yii configuration file located ad /protected/config/main.php and edited the connection to MySQL:

    'db'=>array(
      'connectionString' => 'mysql:host=localhost;dbname=yiitest',
      'emulatePrepare' => true,
      'username' => 'yiitest',
      'password' => 'password',
      'charset' => 'utf8',
    ),
    

    Step 5 - download dburlmanager Yii extension

    Go to Yii dburlmanager, download the Yii dburlmanager extension http://www.yiiframework.com/extension/dburlmanager/ and extract it to your /protected/extensions folder

    Step 6 - Create MySQL database tables and add dummy data

    CREATE TABLE IF NOT EXISTS `articles` (
      `seoURL` varchar(100) NOT NULL
    ) ENGINE=InnoDB;
    
    INSERT INTO `articles` (`seoURL`) VALUES
    ('first-post'),
    ('another-post'),
    ('post/value'),
    ('website/page1');
    
    CREATE TABLE IF NOT EXISTS `pages` (
      `seoURL` varchar(100) NOT NULL
    ) ENGINE=InnoDB;
    
    INSERT INTO `pages` (`seoURL`) VALUES
    ('page-first-post'),
    ('page-another-post'),
    ('page/post/value.html'),
    ('page-website/page1');
    

    Step 7 - Create your Yii custom Controllers

    Create under /protected/controllers folder two php files named ArticleController.php and PageController.php:

    ArticleController.php content:

    render('view', array(
          'article' => isset($_GET['article'])?$_GET['article']:'',
        ));
      }
    }
    

    PageController.php content:

    render('view', array(
          'page' => isset($_GET['page'])?$_GET['page']:'',
        ));
      }
    }
    

    Step 8 - create your custom Yii views

    Create your view files corresponding to those controllers above with the path /protected/views/article/view.php and /protected/views/page/view.php:

    Article view content:

    Article View Test


    Page view content:

    Page View Test


    Step 9 - add custom Yii url rules

    Open again your main.php Yii config file and set your urlManager to something similar to:

    'urlManager'=>array(
      'urlFormat'=>'path',
      'class'=>'ext.DbUrlManager.EDbUrlManager',
      'connectionID'=>'db',
      'rules'=>array(
        ''=>array(
          'article/view',
          'type'=>'db',
          'fields'=>array(
            'article'=>array(
              'table'=>'articles',
              'field'=>'seoURL'
            ),
          ),
        ),
    
        ''=>array(
          'page/view',
          'type'=>'db',
          'fields'=>array(
            'page'=>array(
              'table'=>'pages',
              'field'=>'seoURL'
            ),
          ),
        ),
    
        '/' => '/view',
        '//' => '/',
        '/' => '/',
      ),
      'showScriptName'=>false,
    ),
    

    Step 10 - create .htaccess file

    Create a .htaccess file under your web app root and etid its content to:

    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
    

    Step 11 - test your SEO Friendly URLs

    dev.yiiblog.com/first-post
    dev.yiiblog.com/page-first-post
    

    etc

    Have fun creating awesome blogs or other web apps with complete url managing power.

提交回复
热议问题