【问题标题】:CakePHP: Prefix in URL for each teamCakePHP:每个团队的 URL 中的前缀
【发布时间】:2014-10-12 17:28:08
【问题描述】:

我正在开发一个 CakePHP 3 网站。我想展示基于团队的内容。在每个控制器中,我想访问当前团队。

例如:

www.url.com/team1 --> Team = team1,Controller = Home,Action = Index

www.url.com/team2 --> Team = team2,Controller = Home,Action = Index

www.url.com/team3 --> Team = team3,Controller = Home,Action = Index

...

我想为所有团队使用相同的控制器,但显示的内容是基于团队的。最好的方法是什么?路由似乎对我不起作用,我玩了一下。

【问题讨论】:

  • 路由系统工作正常,这几乎是这里唯一的选择。你做错了什么。
  • 你为什么不把它转过来呢? www.url.com/home/index/team1 然后将team1作为参数传入。你也可以选择将第一个参数路由为团队,但是如果你有一个像 /contact cakephp 这样的 url 将不知道它是一个团队还是一个联系页面。

标签: php cakephp cakephp-3.0


【解决方案1】:
// HomeController.php
public function index($team = null) {
    echo 'Hello Team ' . $team;
}

// routes.php
Router::connect(
    '/team:id', 
    ['controller' => 'Home', 'action' => 'Index'],
    ['id' => '[0-9]+']
);

Reference

【讨论】:

    【解决方案2】:

    只需使用您的 routes.php :)

    在 routes.php 中:

    Router::connect('/team/*', array('controller'=>'Home', 'action'=>'index'));
    In products_controller.php:
    

    在Controller.php中

    function index($id = null) {
        echo "Team" . $id;
    }
    

    http://book.cakephp.org/2.0/en/development/routing.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-22
      • 2016-05-16
      • 1970-01-01
      • 1970-01-01
      • 2018-05-01
      • 2011-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多