StartMVC开发手册

可以快速上手的开发文档

URL&伪静态

默认URL

StartMVC默认的URL结构形式为://serverName/index.php/模块/控制器/方法/参数1/参数2/.../参数n

缺省值

模块、控制器和操作方法都可以在/config/common.php中设置默认值,分别为:

'DefaultModule' => 'Home',      //默认模块,单模块可以不填
'DefaultController' => 'Index', //默认控制器
'DefaultAction' => 'index',     //默认方法<br>

DefaultModule、DefaultController、DefaultAction。根据默认值:

//serverName/index.php 相当于//serverName/index.php/home/index/index

//serverName/index.php/admin 相当于//serverName/index.php/admin/index/index

//serverName/index.php/admin/login 相当于//serverName/index.php/admin/login/index


伪静态

在/config/common.php中设置UrlRewrite为true,开启伪静态,可以隐藏URL中的index.php。

例如://serverName/index.php/home/index/index可以改写为//serverName/home/index/index。

Apache规则(.htaccess)同样适用于IIS6

<ifmodule mod_rewrite.c="">
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,PT,L]
</ifmodule>


Nginx规则

location /{
    if (!-e $request_filename) {
       rewrite  ^/(.*)$  /index.php/$1  last;
       break;
    }
}<br>


IIS7+规则

<rewrite> <rewrite>
  <rules>
    <rule name="rewrite" stopProcessing="true">
      <match url="^(.*)$" ignoreCase="false" />
      <conditions logicalGrouping="MatchAll">
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
      </conditions>
      <action type="Rewrite" url="index.php/{R:1}" appendQueryString="true" />
    </rule>
    </rules>
</rewrite></rewrite>

URL后缀

通过修改/config/common.php中的UrlSuffix来设置URL后缀,可以伪装成任意后缀文件格式。

// config/common.php
return [
    // URL后缀
    'UrlSuffix' => '.html',
    //开启伪静态
    'UrlRewrite' => true,
];
// URL://serverName/home/index/index.html<br>


生成URL

在控制器、模型或视图中,通过$this->url()方法,可以生成绝对路径的Url。

$this->url('home');    // /home
$this->url('home/index/index');    // /home/index/index<br>

$this->Url()只是用来处理伪静态和后缀的,并对URL中的特殊字符进行转码。它并不能根据路由表或路由名来生成URL。