Yii 高级 url 重写 [英] Yii advanced url rewrite

查看:18
本文介绍了Yii 高级 url 重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意 ANYTHING_ELSE所以,我有我的控制器和动作,我想在响应这样的例子时表现得正常:

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 with actionList and actionEdit
user/list
user/edit/25

但是对于不属于特定控制器和操作的所有内容,我希望它们属于一个默认控制器和操作,例如:BlogController 和 actionView.这就是 ANYTHING_ELSE 来的地方.

But for everything that doesn't fall under specific controllers and actions i want them to fall under one default controller and action like: BlogController and actionView. That is where ANYTHING_ELSE comes.

// ANYTHING_ELSE can be:
this-is-a-test-page
this/is/another/page/with/lots/of/slashes
this-has-extension.html


'urlManager' => array(
  '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>',
    'ANYTHING_ELSE' => 'blog/view',
   ),
),

推荐答案

我将逐步解释如何让这个工作.

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

第一步 - 创建一个 Yii 网络应用

Step 1 - Create an Yii web app

在控制台中导航到您的 Yii 框架路径并创建一个新的 web 应用程序.就我而言,我在控制台中使用了它:

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

其中 c:\zeus\yii-1.1.10.r3566\framework 是我的 Yii php 框架路径,而 c:\zeus\www\yiiblog 是我的 Yii webapp 测试文件夹的路径

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

第 2 步 - 将我的域名伪造到 dev.yiiblog.com

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

转到 C:\Windows\System32\drivers\etc 并通过添加以下行来编辑您的主机文件:

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

127.0.0.1 dev.yiiblog.com

第 3 步 - 更改 apache httpd.conf 文件

Step 3 - alter apache httpd.conf file

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

并重启apache服务.我在我的 Windows 控制台中使用过:

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

net stop apache
net start apache

我的 Apache 2 服务被命名为apache",而不是默认的apache2.2".

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

第 4 步 - 创建数据库并配置数据库连接到 Yii

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

我已经创建了一个数据库 yiitest 和一个用户 yiitest.然后我打开了位于 ad/protected/config/main.php 的 Yii 配置文件并编辑了与 MySQL 的连接:

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',
),

步骤 5 - 下载 dburlmanager Yii 扩展

Step 5 - download dburlmanager Yii extension

进入 Yii dburlmanager,下载 Yii dburlmanager 扩展 http://www.yiiframework.com/extension/dburlmanager/ 并将其解压缩到您的/protected/extensions 文件夹

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

步骤 6 - 创建 MySQL 数据库表并添加虚拟数据

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');

第 7 步 - 创建您的 Yii 自定义控制器

Step 7 - Create your Yii custom Controllers

在/protected/controllers文件夹下创建两个php文件,分别命名为ArticleController.php和PageController.php:

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

ArticleController.php 内容:

ArticleController.php content:

<?php
/**
 * @filename ArticleController.php
 */

class ArticleController extends CController {
  public function actionView() {
    $this->render('view', array(
      'article' => isset($_GET['article'])?$_GET['article']:'',
    ));
  }
}

PageController.php 内容:

PageController.php content:

<?php
/**
 * @filename PageController.php
 */
class PageController extends CController {
  public function actionView() {
    $this->render('view', array(
      'page' => isset($_GET['page'])?$_GET['page']:'',
    ));
  }
}

第 8 步 - 创建自定义 Yii 视图

Step 8 - create your custom Yii views

使用路径/protected/views/article/view.php 和/protected/views/page/view.php 创建与上述控制器对应的视图文件:

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

文章查看内容:

<h1>Article View Test</h1>
<br />
<?php
    if (isset ($article)) echo "article: $article";
?>

页面浏览内容:

<h1>Page View Test</h1>
<br />
<?php
    if (isset ($page)) echo "page: $page";
?>

步骤 9 - 添加自定义 Yii url 规则

Step 9 - add custom Yii url rules

再次打开您的 main.php Yii 配置文件并将您的 urlManager 设置为类似于:

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(
    '<article:[\w\/.-]+>'=>array(
      'article/view',
      'type'=>'db',
      'fields'=>array(
        'article'=>array(
          'table'=>'articles',
          'field'=>'seoURL'
        ),
      ),
    ),

    '<page:[\w\/.-]+>'=>array(
      'page/view',
      'type'=>'db',
      'fields'=>array(
        'page'=>array(
          'table'=>'pages',
          'field'=>'seoURL'
        ),
      ),
    ),

    '<controller:\w+>/<id:\d+>' => '<controller>/view',
    '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
    '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
  ),
  'showScriptName'=>false,
),

第 10 步 - 创建 .htaccess 文件

Step 10 - create .htaccess file

在您的 Web 应用根目录下创建一个 .htaccess 文件并将其内容确定为:

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

第 11 步 - 测试您的 SEO 友好网址

Step 11 - test your SEO Friendly URLs

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

使用完整的 url 管理功能创建精彩的博客或其他网络应用程序,玩得开心.

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

这篇关于Yii 高级 url 重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆