c语言编程笔录

首页 >   > 笔记大全

笔记大全

php怎么使用Yii路由进行URL路由

更新时间:2023-10-09

关于使用Yii路由进行URL路由

1. Yii路由介绍

Yii是一个高效的PHP框架,它提供了丰富的功能和优秀的性能,其中之一就是路由,Yii路由可将URL转换为Controller,Action和参数。Yii路由是使用配置来管理的,其中还包括路由规则、参数、匹配内容、默认值等等。 Yii框架的默认路由规则很简单,controller/action/参数1/参数2/...,但是开发者还可以自己定义和修改路由规则来满足自己的需求。

2. Yii路由使用方法

在Yii框架中,路由规则都存储在应用程序的config/main.php文件中。通过修改main.php配置文件,就可以配置应用程序的路由规则。 Yii路由通常包括三部分:路由规则、ControllerID和 ActionsID。

'components'=> [
    'urlManager'=> [
        'enablePrettyUrl'=> true,
        'showScriptName'=> false,
        'rules' => [
           'posts'     => 'post/index',
           'posts/'=> 'post/view',
           'posts//'=> 'post/viewByTitle',
           'posts/edit/<id:\d+>'=> 'post/edit',
           'posts/delete/<id:\d+>'=> 'post/delete',
        ],
      ],
  ],
</pre>
<p>上面的rules就是路由规则,可以根据需求进行定义和修改,ControllerID代表控制器类的名称,ActionID代表控制器类中的一个方法名。例如,上面的配置规则中,posts规则的值为post/index,表示posts将被转换为对应Controller类中的index方法。</p>

<h4>3. Yii路由常用用法示例</h4>
<p>以下是一些常用的Yii路由使用方法示例:</p>
<p><strong>参数传递</strong></p>
<pre class="brush: php;toolbar:false">
'urlManager'=> [
    'enablePrettyUrl'=> true,
    'showScriptName'=> false,
    'rules'=> [
        'blog/<id:\d+>'=> 'blog/view',
        'blog/update/<id:\d+>'=> 'blog/update',
        'blog/delete/<id:\d+>'=> 'blog/delete',
    ],
],
</pre>
<p>在这个示例中,我们使用了占位符来传递参数id。例如,如果URL为/blog/123,将转化为controller/action,并且$id变量将包含URL中传入的123的值。</p>

<p><strong>默认控制器和动作</strong></p>
<pre class="brush: php;toolbar:false">
'urlManager'=> [
    'enablePrettyUrl'=> true,
    'showScriptName'=> false,
    'rules'=> [
        ''=> 'site/index',
        'about'=> 'site/about',
        'contact'=> 'site/contact',
        'login'=> 'site/login',
        'logout'=> 'site/logout',
    ],
],
</pre>
<p>在这个示例中,我们设置了默认控制器和动作,访问网站主页时,默认访问site/index控制器,不传递任何参数。如果网站url输入about或contact,则调用site/about或site/contact控制器。同样地,登录和注销请求将分别调用site/login和site/logout控制器。</p>

<h4>4. Yii路由的总结</h4>
<p>Yii路由是一个非常强大和灵活的工具,实现了URL到控制器之间高效的匹配过程。 Yii路由通过以下几方面做到了解耦、模块化,易于维护。</p>
<ol>
  <li>支持占位符,可以方便地让URL和控制器之间实现参数传递。</p>
  <li>支持正则表达式,并且支持URL解析。</p>
  <li>支持自定义和修改路由规则。</p>
  <li>可以设置默认控制器和动作,简化了应用程序的开发。</p>
</ol>
        </div>
        <div class="share" id="down">          <div class="share-text">
            <p>本文如果侵犯了你的权益请联系站长整改删除</p>
            <p>转载请注明出处</p>
            <p>本文地址:<a href="https://www.radbuilder.com/marketing/Python/14893.html" target="_blank">https://www.radbuilder.com/marketing/Python/14893.html</a></p>
          </div>
        </div>
      </div>
    </div>
    <div class="clear blank"></div>
    <div class="down-links whitebg">
    <div class="news-title">
        <h2></h2>
      </div>
      <ul>

     </ul>
    </div>
     <div class="clear blank"></div>
    <div class="down-otherlink whitebg">
      <div class="news-title">
        <h2>图文推荐</h2>
      </div>
      <ul>
        <li><a href="https://www.radbuilder.com/marketing/Python/2052.html" target="_blank"><i><img src="/d/file/p/2023/07-01/small6647a07de5915ca25dce66845345e784.jpg"></i>
          <p>如何在php中对json对象的值进行输出</p>
          <span class="down-info"></span></a></li>
        <li><a href="https://www.radbuilder.com/marketing/Python/2007.html" target="_blank"><i><img src="/d/file/p/2023/07-01/small1c14fee124197f8a8d8d542f6468c434.jpg"></i>
          <p>jquery min js指的是什么</p>
          <span class="down-info"></span></a></li>
        <li><a href="https://www.radbuilder.com/marketing/Python/2049.html" target="_blank"><i><img src="/d/file/p/2023/07-01/small3b228fd92e9008dce070ca2578c632ff.jpg"></i>
          <p>正在执行的SQL语句怎么在postgresql中结束</p>
          <span class="down-info"></span></a></li>
        <li><a href="https://www.radbuilder.com/marketing/Python/1995.html" target="_blank"><i><img src="/d/file/p/2023/07-01/small1b9fcb9991e07208298c92968b167cbe.jpg"></i>
          <p>PHP中is+numeric与ctype+digit有什么不同</p>
          <span class="down-info"></span></a></li>
        <li><a href="https://www.radbuilder.com/marketing/Python/2109.html" target="_blank"><i><img src="/d/file/p/2023/07-02/small27f3e50affd2acd5029588ed2c1111ab.jpg"></i>
          <p>利用php如何对非法字符进行过滤</p>
          <span class="down-info"></span></a></li>
        <li><a href="https://www.radbuilder.com/marketing/Python/2102.html" target="_blank"><i><img src="/d/file/p/2023/07-02/smalle75ad5f22e9a604e3a9a83f0dd057d19.jpg"></i>
          <p>怎么在Java中使用LinkedList</p>
          <span class="down-info"></span></a></li>
      </ul>
    </div>

    <div class="pinglun-box whitebg">
      <div class="news-title">
        <h2></h2>
      </div>

    </div>
  </div>
  <aside class="side-section right-box">
     <div class="whitebg down-tuijian">

    </div>
    <div class="blank clear" ></div>
    <div class="whitebg down-paihang">
      <h2 class="side-title">热门排行</h2>
      <ul>
        <li><i></i><a href="https://www.radbuilder.com/marketing/Python/2052.html" title="如何在php中对json对象的值进行输出" target="_blank">如何在php中对json对象的值进行输出</a></li>
        <li><i></i><a href="https://www.radbuilder.com/marketing/Python/1174.html" title="uniapp实现定位权限" target="_blank">uniapp实现定位权限</a></li>
        <li><i></i><a href="https://www.radbuilder.com/marketing/Python/1244.html" title="Python实现热加载配置文件的方法" target="_blank">Python实现热加载配置文件的方法</a></li>
        <li><i></i><a href="https://www.radbuilder.com/marketing/Python/3489.html" title="ps如何把皮肤通透白嫩" target="_blank">ps如何把皮肤通透白嫩</a></li>
        <li><i></i><a href="https://www.radbuilder.com/marketing/Python/11784.html" title="js中怎么用文件流下载csv文件" target="_blank">js中怎么用文件流下载csv文件</a></li>
        <li><i></i><a href="https://www.radbuilder.com/marketing/Python/989.html" title="怎么使用PHP进行人工智能开发" target="_blank">怎么使用PHP进行人工智能开发</a></li>
        <li><i></i><a href="https://www.radbuilder.com/marketing/Python/1008.html" title="XML文档不能使用css样式表如何办" target="_blank">XML文档不能使用css样式表如何办</a></li>
        <li><i></i><a href="https://www.radbuilder.com/marketing/Python/1257.html" title="JavaScript中的错误处理技巧" target="_blank">JavaScript中的错误处理技巧</a></li>
      </ul>
    </div>
    <div class="blank clear" ></div>
    <div class="whitebg cloud">
      <h2 class="side-title">标签云</h2>
      <ul>
     <a href="https://www.radbuilder.com/e/tags/?tagid=73&tempid=8" target="_blank">PHP(1)</a> <a href="https://www.radbuilder.com/e/tags/?tagid=64&tempid=8" target="_blank">C(6)</a> <a href="https://www.radbuilder.com/e/tags/?tagid=102&tempid=8" target="_blank">c语言(750)</a> <a href="https://www.radbuilder.com/e/tags/?tagid=63&tempid=8" target="_blank">C++(7)</a>      </ul>
    </div>
    <div class="ad ad-small"></div>
    <div class="whitebg down-suiji">
      <h2 class="side-title">猜你喜欢</h2>
      <ul>
                  
        <li><a target="_blank"  href="https://www.radbuilder.com/marketing/Python/5374.html"><i><img src=""></i>
          <p>怎么将工程托管到gitee</p>
          <span class="down-info"></span></a></li>
           
        <li><a target="_blank"  href="https://www.radbuilder.com/marketing/Python/4977.html"><i><img src=""></i>
          <p>thinkphp如何返回某几条数据</p>
          <span class="down-info"></span></a></li>
           
        <li><a target="_blank"  href="https://www.radbuilder.com/marketing/Python/9528.html"><i><img src=""></i>
          <p>Java中怎么使用Collection类</p>
          <span class="down-info"></span></a></li>
           
        <li><a target="_blank"  href="https://www.radbuilder.com/marketing/Python/8167.html"><i><img src=""></i>
          <p>ps如何看图片尺寸大小</p>
          <span class="down-info"></span></a></li>
           
        <li><a target="_blank"  href="https://www.radbuilder.com/marketing/Python/3349.html"><i><img src=""></i>
          <p>Vue3如何实现组件级基类</p>
          <span class="down-info"></span></a></li>
           
        <li><a target="_blank"  href="https://www.radbuilder.com/marketing/Python/3514.html"><i><img src=""></i>
          <p>bootstrap怎么设置鼠标悬停提示</p>
          <span class="down-info"></span></a></li>
           
        <li><a target="_blank"  href="https://www.radbuilder.com/marketing/Python/1187.html"><i><img src=""></i>
          <p>怎么在Symfony框架中使用控制器的参数</p>
          <span class="down-info"></span></a></li>
           
        <li><a target="_blank"  href="https://www.radbuilder.com/marketing/Python/11360.html"><i><img src=""></i>
          <p>ps如何让文字环绕内圆</p>
          <span class="down-info"></span></a></li>
           
        <li><a target="_blank"  href="https://www.radbuilder.com/marketing/Python/8062.html"><i><img src=""></i>
          <p>前端vue .js是什么</p>
          <span class="down-info"></span></a></li>
           
        <li><a target="_blank"  href="https://www.radbuilder.com/marketing/Python/13869.html"><i><img src=""></i>
          <p>web用php如何实现搜索功能</p>
          <span class="down-info"></span></a></li>
           </ul>
    </div>
  </aside>
</article>
<div class="clear blank"></div>
<footer>
  <div class="footer box">
    <div class="wxbox">
      <ul>
        <li><span> </span></li>
        <li><span> </span></li>
      </ul>
    </div>
    <div class="endnav">
      <p>备案号:<a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">粤ICP备2023061792号-2</a> <a href="https://www.radbuilder.com/sitemap.xml" target="_blank">网站地图</a></p>
    </div>
  </div>
</footer>
<div class="toolbar-open"></div>
<div class="toolbar">
  <div class="toolbar-close"><span id="closed"></span></div>
  <div class="toolbar-nav">
    <ul id="toolbar-menu">
      <li><i class="side-icon-user"></i>
        <section>
          <div class="userinfo">
              <script src="https://www.radbuilder.com/e/member/login/loginjs.php"></script>
          </div>
        </section>
      </li>
      <li><i class="side-icon-qq"></i>
        <section class="qq-section">
          <div class="qqinfo"><a href="http://wpa.qq.com/msgrd?v=3&uin=19801987&site=qq&menu=yes">站长QQ</a></div>
        </section>
      </li>
      <li><i class="side-icon-weixin"></i>
        <section class="weixin-section">
          <div class="weixin-info">
            <p>个人微信</p>
            <p class="text12">工作时间</p>
            <p class="text12">周一至周日 9:00-21:00</p>
          </div>
        </section>
      </li>
      <li><i class="side-icon-dashang"></i>
        <section class="dashang-section">
          <p></p>
          <ul>
            <li></li>
            <li></li>
          </ul>
        </section>
      </li>
    
    </ul>
  </div>
</div>
<div class="endmenu">
<ul>
<li><a href="https://www.radbuilder.com/"><i class="iconfont icon-shouye"></i>首页</a></li>
<li><a href="https://www.radbuilder.com/phone-fenlei.html"><i class="iconfont icon-fenlei"></i>分类</a></li>
<li><a href="https://www.radbuilder.com/phone-list.html"><i class="iconfont icon-navicon-wzgl"></i>所有</a></li>
<li><a href="https://www.radbuilder.com/e/member/my/"><i class="iconfont icon-My"></i>我的</a></li>
</ul>
</div>
<a href="#" title="返回顶部" class="icon-top"></a>
</body>
</html><script src="https://www.radbuilder.com/e/public/onclick/?enews=donews&classid=20&id=14893"></script>