YII如何使用url组件美化管理
创始人
2024-12-11 01:45:35
0次

具体如下:

urlManager组件

yii的官方文档对此的解释如下:

urlSuffix  此规则使用的url后缀,默认使用CurlManger::urlSuffix,值为null。例如可以将此设置为.html,让url看起来“像”是一个静态页面。
caseSensitive  是否大小写敏感,默认使用CUrlManager::caseSensitive,值为null。
defaultParams  该规则使用的默认get参数。当使用该规则来解析一个请求时,这个参数的值会被注入到$_GET参数中。
matchValue  当创建一个URL时,GET参数是否匹配相应的子模式。默认使用CurlManager::matchValue,值为null。

如果该属性为 false,那么意味着当路由和参数名匹配给定的规则时,将以此来创建一个URL。

如果该属性为true,那么给定的参数值夜必须匹配相应的参数子模式。

注意:将此属性设置为true会降低性能。

我们使用一些例子来解释网址工作规则。我们假设我们的规则包括如下三个:

array(
  'posts'=>'post/list',
  'post/'=>'post/read',
  'post//'=>'post/read',
)</pre><p>调用$this->createUrl('post/list')生成/index.php/posts。第一个规则适用。</p><p>调用$this->createUrl('post/read',array('id'=>100))生成/index.php/post/100。第二个规则适用。</p><p>调用$this->createUrl('post/read',array('year'=>2008,'title'=>'a sample post'))生成/index.php/post/2008/a%20sample%20post。第三个规则适用。</p><p>调用$this->createUrl('post/read')产生/index.php/post/read。请注意,没有规则适用。</p><p>总之,当使用createUrl生成网址,路线和传递给该方法的GET参数被用来决定哪些网址规则适用。如果关联规则中的每个参数可以在GET参数找到的,将被传递给createUrl ,如果路线的规则也匹配路线参数,规则将用来生成网址。</p><p>如果GET参数传递到createUrl是以上所要求的一项规则,其他参数将出现在查询字符串。例如,如果我们调用$this->createUrl('post/read',array('id'=>100,'year'=>2008)) ,我们将获得/index.php/post/100?year=2008。为了使这些额外参数出现在路径信息的一部分,我们应该给规则附加/* 。 因此,该规则post/<id:\d+>/* ,我们可以获取网址/index.php/post/100/year/2008 。</p><p>正如我们提到的,URL规则的其他用途是解析请求网址。当然,这是URL生成的一个逆过程。例如, 当用户请求/index.php/post/100 ,上面例子的第二个规则将适用来解析路线post/read和GET参数array('id'=>100) (可通过$_GET获得) 。</p><p>提示:此网址通过createurl方法所产生的是一个相对地址。为了得到一个绝对的url ,我们可以用前缀yii: :app()->hostInfo ,或调用createAbsoluteUrl 。</p><p>注:使用的URL规则将降低应用的性能。这是因为当解析请求的URL ,[ CUrlManager ]尝试使用每个规则来匹配它,直到某个规则可以适用。因此,高流量网站应用应尽量减少其使用的URL规则。</p><p>test.com/vthot 想生成 test.com/vthot/<br/></p><p><span >复制代码</span> 代码如下:</p><p>'urlSuffix'=>'/',</p><p><br/>要更改URL格式,我们应该配置urlManager应用元件,以便createUrl可以自动切换到新格式和应用程序可以正确理解新的网址:</p><pre class="brush:php;">'urlManager'=>array(
  'urlFormat'=>'path',
  'showScriptName'=>false,
  'urlSuffix'=>'.html',
  'rules'=>array(
    'posts'=>'post/list',
    'post/<id:\d+>'=>array('post/show','urlSuffix'=>'.html'),
    'post/<id:\d+>/<mid:\w+>'=>array('post/view','urlSuffix'=>'.xml'),
  ),
),</pre><p>示例一</p><p>Rule代码<br/></p><p><span >复制代码</span> 代码如下:</p><p>'posts'=>'post/list',</p><p><br/>Action代码<br/></p><p><span >复制代码</span> 代码如下:</p><p>echo $this->createAbsoluteUrl('post/list');</p><p>输出</p><p>http://localhost/test/index.php/post</p><p>示例二</p><p>Rule代码<br/></p><p><span >复制代码</span> 代码如下:</p><p>'post/<id:\d+>'=>array('post/show','urlSuffix'=>'.html'),</p><p><br/>Action代码<br/></p><p><span >复制代码</span> 代码如下:</p><p>echo $this->createAbsoluteUrl('post/show',array('id'=>998, 'name'=>'123'));</p><p>输出</p><p>http://localhost/test/index.php/post/998.html?name=123</p><p>示例三</p><p>Rule代码:<br/></p><p><span >复制代码</span> 代码如下:</p><p>'post/<id:\d+>/<mid:\w+>'=>array('post/view','urlSuffix'=>'.xml'),</p><p>Action代码<br/></p><p><span >复制代码</span> 代码如下:</p><p>echo $this->createAbsoluteUrl('post/view',array('id'=>998, 'mid'=>'tody'));</p><p><br/>输出</p><p>http://localhost/test/index.php/post/998/tody.xml</p><p>示例四</p><p>Rule代码<br/></p><p><span >复制代码</span> 代码如下:</p><p>'http://<user:\w+>.vt.com/<_c:(look|seek)>'=>array('<_c>/host','urlSuffix'=>'.me'),</p><p>Action代码:</p><pre class="brush:php;">echo $this->createAbsoluteUrl('look/host',array('user'=>'boy','mid'=>'ny-01'));
echo '';
echo $this->createAbsoluteUrl('looks/host',array('user'=>'boy','mid'=>'ny-01'));</pre><p>输出</p><p>http://boy.vt.com/look.me?mid=ny-01<br/>http://localhost/test/index.php/looks/host/user/boy/mid/ny-01</p><p>1)controller/Update/id/23</p><pre class="brush:php;">public function actionUpdate(){
  $id = Yii::app()->request->getQuery('id') ; 经过处理的$_GET['id']
}
//$id = Yii::app()->request->getPost('id'); 经过处理的$_POST['id']
//$id = Yii::app()->request->getParam('id'); //CHttpRequest更多</pre><p>2)public function actionUpdate($id)  这种不支持多主键,会检查一下到底GET里面有没有id,没有id就直接不允许访问</p><pre class="brush:php;">'sayhello/<name>' => 'post/hello', name是PostController actionHello($name)的参数
'post/<alias:[-a-z]+>' => 'post/view',  domain/post/e文小写 其中:前面的alias是PostController actionView($alias)的参数
'(posts|archive)/<order:(DESC|ASC)>' => 'post/index', domain/posts/DESC或domain/posts/ASC
'(posts|archive)' => 'post/index', domain/posts或domain/archive
'tos' => array('website/page', 'defaultParams' => array('alias' =>'terms_of_service')),</pre><p>When the URL is /tos, pass terms_of_service as the alias parameter value.</p><p><strong>隐藏 index.php</strong></p><p>还有一点,我们可以做进一步清理我们的网址,即在URL中藏匿index.php  入口脚本。这就要求我们配置Web服务器,以及urlManager应用程序元件。</p><p>1.add showScriptName=>false</p><p>2.add project/.htaccess</p><pre class="brush:xhtml;">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</pre><p>3.开启rewrite</p><p>简单的说,在main.php中简单设置urlManager,然后讲了3条规则,基本都覆盖到了。最后是隐藏index.php,请记住.htaccess位于index.php同级目录 ,而不是protected/目录。其他就简单了。</p>                <!--end::Text-->
            </div>
            <!--end::Description-->
            <div class="mt-5">
                <!--关键词搜索-->
                            </div>
            <div class="mt-5">
                <p class="fc-show-prev-next">
                    <strong>上一篇:</strong><a href="/kaifa/show-14064065.html">Yii如何实现单用户博客系统文章详情页插入评论表单</a><br>
                </p>
                <p class="fc-show-prev-next">
                    <strong>下一篇:</strong><a href="/kaifa/show-14064067.html">yii怎么去掉必填项中星号</a>                </p>
            </div>
            <!--begin::Block-->
            <div class="d-flex flex-stack mb-2 mt-10">
                <!--begin::Title-->
                <h3 class="text-dark fs-5 fw-bold text-gray-800">相关内容</h3>
                <!--end::Title-->
            </div>
            <div class="separator separator-dashed mb-9"></div>
            <!--end::Block-->
            <div class="row g-10">
                

            </div>


        </div>
        <!--end::Table widget 14-->
    </div>
    <!--end::Col-->

    <!--begin::Col-->
    <div class="col-xl-4 mt-0">
        <!--begin::Chart Widget 35-->
        <div class="card card-flush h-md-100">
            <!--begin::Header-->
            <div class="card-header pt-5 ">
                <!--begin::Title-->
                <h3 class="card-title align-items-start flex-column">
                    <!--begin::Statistics-->
                    <div class="d-flex align-items-center mb-2">
                        <!--begin::Currency-->
                        <span class="fs-5 fw-bold text-gray-800 ">热门资讯</span>
                        <!--end::Currency-->
                    </div>
                    <!--end::Statistics-->
                </h3>
                <!--end::Title-->
            </div>
            <!--end::Header-->
            <!--begin::Body-->
            <div class="card-body pt-3">

                                <!--begin::Item-->
                <div class="d-flex flex-stack mb-7">
                    <!--begin::Symbol-->
                    <div class="symbol symbol-60px symbol-2by3 me-4">
                        <div class="symbol-label" style="background-image: url('/uploadfile/[db:缩略图]')"></div>
                    </div>
                    <!--end::Symbol-->
                    <!--begin::Title-->
                    <div class="m-0">
                        <a href="/weixiu/show-14537931.html" class="text-dark fw-bold text-hover-primary fs-6">空调连续工作多久需要停机休息</a>
                        <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...</span>
                    </div>
                    <!--end::Title-->
                </div>
                                <!--begin::Item-->
                <div class="d-flex flex-stack mb-7">
                    <!--begin::Symbol-->
                    <div class="symbol symbol-60px symbol-2by3 me-4">
                        <div class="symbol-label" style="background-image: url('/uploadfile/[db:缩略图]')"></div>
                    </div>
                    <!--end::Symbol-->
                    <!--begin::Title-->
                    <div class="m-0">
                        <a href="/weixiu/show-14537930.html" class="text-dark fw-bold text-hover-primary fs-6">空调哪个符号是除湿的</a>
                        <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...</span>
                    </div>
                    <!--end::Title-->
                </div>
                                <!--begin::Item-->
                <div class="d-flex flex-stack mb-7">
                    <!--begin::Symbol-->
                    <div class="symbol symbol-60px symbol-2by3 me-4">
                        <div class="symbol-label" style="background-image: url('/uploadfile/[db:缩略图]')"></div>
                    </div>
                    <!--end::Symbol-->
                    <!--begin::Title-->
                    <div class="m-0">
                        <a href="/weixiu/show-14537929.html" class="text-dark fw-bold text-hover-primary fs-6">格力空调多长时间加一次氟利昂</a>
                        <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...</span>
                    </div>
                    <!--end::Title-->
                </div>
                                <!--begin::Item-->
                <div class="d-flex flex-stack mb-7">
                    <!--begin::Symbol-->
                    <div class="symbol symbol-60px symbol-2by3 me-4">
                        <div class="symbol-label" style="background-image: url('/uploadfile/[db:缩略图]')"></div>
                    </div>
                    <!--end::Symbol-->
                    <!--begin::Title-->
                    <div class="m-0">
                        <a href="/weixiu/show-14537928.html" class="text-dark fw-bold text-hover-primary fs-6">空调用多长时间需要加氟一次</a>
                        <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...</span>
                    </div>
                    <!--end::Title-->
                </div>
                                <!--begin::Item-->
                <div class="d-flex flex-stack mb-7">
                    <!--begin::Symbol-->
                    <div class="symbol symbol-60px symbol-2by3 me-4">
                        <div class="symbol-label" style="background-image: url('/uploadfile/202609/83c0c0800a58e3b.jpg')"></div>
                    </div>
                    <!--end::Symbol-->
                    <!--begin::Title-->
                    <div class="m-0">
                        <a href="/news/show-14537927.html" class="text-dark fw-bold text-hover-primary fs-6">格调生活·C标思享荟:卡萨帝在...</a>
                        <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...</span>
                    </div>
                    <!--end::Title-->
                </div>
                                <!--begin::Item-->
                <div class="d-flex flex-stack mb-7">
                    <!--begin::Symbol-->
                    <div class="symbol symbol-60px symbol-2by3 me-4">
                        <div class="symbol-label" style="background-image: url('/uploadfile/202609/bd5bdfc6ba7da8c.jpg')"></div>
                    </div>
                    <!--end::Symbol-->
                    <!--begin::Title-->
                    <div class="m-0">
                        <a href="/news/show-14537926.html" class="text-dark fw-bold text-hover-primary fs-6">品牌价值破千亿、高端份额持续领...</a>
                        <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...</span>
                    </div>
                    <!--end::Title-->
                </div>
                                <!--begin::Item-->
                <div class="d-flex flex-stack mb-7">
                    <!--begin::Symbol-->
                    <div class="symbol symbol-60px symbol-2by3 me-4">
                        <div class="symbol-label" style="background-image: url('/uploadfile/[db:缩略图]')"></div>
                    </div>
                    <!--end::Symbol-->
                    <!--begin::Title-->
                    <div class="m-0">
                        <a href="/weixiu/show-14537925.html" class="text-dark fw-bold text-hover-primary fs-6">空调主板坏了修800还是换新的</a>
                        <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...</span>
                    </div>
                    <!--end::Title-->
                </div>
                                <!--begin::Item-->
                <div class="d-flex flex-stack mb-7">
                    <!--begin::Symbol-->
                    <div class="symbol symbol-60px symbol-2by3 me-4">
                        <div class="symbol-label" style="background-image: url('/uploadfile/[db:缩略图]')"></div>
                    </div>
                    <!--end::Symbol-->
                    <!--begin::Title-->
                    <div class="m-0">
                        <a href="/weixiu/show-14537924.html" class="text-dark fw-bold text-hover-primary fs-6">空调开多久需要休息多长时间</a>
                        <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...</span>
                    </div>
                    <!--end::Title-->
                </div>
                                <!--begin::Item-->
                <div class="d-flex flex-stack mb-7">
                    <!--begin::Symbol-->
                    <div class="symbol symbol-60px symbol-2by3 me-4">
                        <div class="symbol-label" style="background-image: url('/uploadfile/[db:缩略图]')"></div>
                    </div>
                    <!--end::Symbol-->
                    <!--begin::Title-->
                    <div class="m-0">
                        <a href="/weixiu/show-14537923.html" class="text-dark fw-bold text-hover-primary fs-6">中央空调多长时间清洗一次</a>
                        <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...</span>
                    </div>
                    <!--end::Title-->
                </div>
                                <!--begin::Item-->
                <div class="d-flex flex-stack mb-7">
                    <!--begin::Symbol-->
                    <div class="symbol symbol-60px symbol-2by3 me-4">
                        <div class="symbol-label" style="background-image: url('/uploadfile/[db:缩略图]')"></div>
                    </div>
                    <!--end::Symbol-->
                    <!--begin::Title-->
                    <div class="m-0">
                        <a href="/weixiu/show-14537922.html" class="text-dark fw-bold text-hover-primary fs-6">中央空调氟利昂多久加一次</a>
                        <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...</span>
                    </div>
                    <!--end::Title-->
                </div>
                
            </div>
            <!--end::Body-->
        </div>
        <!--end::Chart Widget 35-->
    </div>
    <!--end::Col-->
</div>



</div>
<!--end::Content container-->
</div>
<!--end::Content-->
</div>
<!--end::Content wrapper-->
<!--begin::Footer-->
<div id="kt_app_footer" class="app-footer">
    <!--begin::Footer container-->
    <div class="app-container container-xxl d-flex flex-column flex-md-row flex-center flex-md-stack py-3">
        <!--begin::Copyright-->
        <div class="text-dark order-2 order-md-1">
            <span class="text-muted fw-semibold me-1">2026 ©</span>
            太卓网<a href="http://www.yexian114.com/">叶县生活网</a> <a href="https://www.wxx86.cn/">编程知识网</a>  <a href="http://www.kcwzh.com/">开创游戏网</a> <a href="https://www.caiding5.net">菜丁网</a><a href="http://cn.office369.com">奥飞商务网</a>  <a href="http://www.zlnznjj.com/">众乐知识网</a><a href="http://www.lhjia.com/">乐活家</a><a href="http://www.nengyuan100.com/">能源100网</a> <a href="http://it.tdroid.net/">泰达科技</a><a href="http://cn.tdroid.net/">太卓开发网</a><a href="http://cn.yuansudz.com/">元素科技网</a><a href="http://www.80hlw.com/">八零商务网</a><a href="http://game.tdroid.net/">土豆游戏网</a>        </div>
        <!--end::Copyright-->
        <!--begin::Menu-->
        <ul class="menu menu-gray-600 menu-hover-primary fw-semibold order-1">
                        <li class="menu-item">
                <a href="/news" target="_blank" class="menu-link px-2">科技资讯</a>
            </li>
                        <li class="menu-item">
                <a href="/jishu" target="_blank" class="menu-link px-2">技术分享</a>
            </li>
                        <li class="menu-item">
                <a href="/kaifa" target="_blank" class="menu-link px-2">程序开发</a>
            </li>
                        <li class="menu-item">
                <a href="/weixiu" target="_blank" class="menu-link px-2">设备维修</a>
            </li>
                    </ul>
        <!--end::Menu-->
    </div>
    <!--end::Footer container-->
</div>
<!--end::Footer-->
</div>
<!--end:::Main-->
</div>
<!--end::Wrapper-->
</div>
<!--end::Page-->
</div>
<!--end::App-->
<div id="kt_scrolltop" class="scrolltop" data-kt-scrolltop="true">
    <!--begin::Svg Icon | path: icons/duotune/arrows/arr066.svg-->
    <span class="svg-icon">
        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
            <rect opacity="0.5" x="13" y="6" width="13" height="2" rx="1" transform="rotate(90 13 6)" fill="currentColor"></rect>
            <path d="M12.5657 8.56569L16.75 12.75C17.1642 13.1642 17.8358 13.1642 18.25 12.75C18.6642 12.3358 18.6642 11.6642 18.25 11.25L12.7071 5.70711C12.3166 5.31658 11.6834 5.31658 11.2929 5.70711L5.75 11.25C5.33579 11.6642 5.33579 12.3358 5.75 12.75C6.16421 13.1642 6.83579 13.1642 7.25 12.75L11.4343 8.56569C11.7467 8.25327 12.2533 8.25327 12.5657 8.56569Z" fill="currentColor"></path>
        </svg>
    </span>
    <!--end::Svg Icon-->
</div>
<!--begin::Javascript-->
<script>var hostUrl = "/static/default/pc/";</script>
<!--begin::Global Javascript Bundle(mandatory for all pages)-->
<script src="/static/default/pc/plugins/global/plugins.bundle.js"></script>
<script src="/static/default/pc/js/scripts.bundle.js"></script>
<!--end::Global Javascript Bundle-->

<!--end::Javascript-->
</body>
<!--end::Body-->
</html>