Php 如何路由相同的Codeigniter控制器

Php 如何路由相同的Codeigniter控制器,php,codeigniter,routing,controller,remap,Php,Codeigniter,Routing,Controller,Remap,我的Codeigniter项目有一长串相同的类别,每个类别都有许多相同的方法 为了使它更加动态和干净,我使用了_remap函数在控制器中加载相同的方法。现在我正在尝试复制控制器 e、 g.我的控制器 羚羊。php蝙蝠。php布谷鸟。php狗。php大象。php。。。php都有以下格式(我使用_remap将所有类似的方法压缩为一个) 您可以尝试以下几个选项: 有一个Animals.php基类,并从中继承所有其他类别,如下所示 Animal.php class Animal extends CI_C

我的Codeigniter项目有一长串相同的类别,每个类别都有许多相同的方法

为了使它更加动态和干净,我使用了_remap函数在控制器中加载相同的方法。现在我正在尝试复制控制器

e、 g.我的控制器 羚羊。php蝙蝠。php布谷鸟。php狗。php大象。php。。。php都有以下格式(我使用_remap将所有类似的方法压缩为一个)


您可以尝试以下几个选项:

  • 有一个Animals.php基类,并从中继承所有其他类别,如下所示
  • Animal.php

    class Animal extends CI_Controller {
        //Your _remap() and other methods
    }
    
    羚羊。php蝙蝠。php布谷鸟。php狗。php大象。php。。。Zebra.php

    require_once 'animal.php';
    
    class Antelope extends Animal {
        //Antelope inherits its methods from Animal, so this is blank 
    } 
    
    此方法要求您拥有100个几乎为空的控制器文件(每个动物/类别一个),因此它是动态的,不是很动态,但允许您在动物基类中实现一个_init()方法,并在子类中重写它,允许您设置类别中的任何偏差(即腿数等)


  • 对所有动物使用一条路线
  • routes.php

    //default index route http://www.sample.com/animal/type
    $route['animal/(:any)']  = 'animal/reroute/$1/index';
    
    //method route http://www.sample.com/animal/type/method
    $route['animal/(:any)/(:any)']  = 'animal/reroute/$1/$2';
    
    animal.php

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Animal extends CI_Controller {
    
        function __construct() {
            parent::__construct();
        }
    
    
        public function index(){
           // Stuff goes here
        }
    
        public function reroute($animal, $method) {
           //Check the $animal is valid
           //Check the $method is valid
           //Do stuff   
        }
    
    
    }
    
    /** End of file animal.php **/
    

    您可以尝试以下几个选项:

  • 有一个Animals.php基类,并从中继承所有其他类别,如下所示
  • Animal.php

    class Animal extends CI_Controller {
        //Your _remap() and other methods
    }
    
    羚羊。php蝙蝠。php布谷鸟。php狗。php大象。php。。。Zebra.php

    require_once 'animal.php';
    
    class Antelope extends Animal {
        //Antelope inherits its methods from Animal, so this is blank 
    } 
    
    此方法要求您拥有100个几乎为空的控制器文件(每个动物/类别一个),因此它是动态的,不是很动态,但允许您在动物基类中实现一个_init()方法,并在子类中重写它,允许您设置类别中的任何偏差(即腿数等)


  • 对所有动物使用一条路线
  • routes.php

    //default index route http://www.sample.com/animal/type
    $route['animal/(:any)']  = 'animal/reroute/$1/index';
    
    //method route http://www.sample.com/animal/type/method
    $route['animal/(:any)/(:any)']  = 'animal/reroute/$1/$2';
    
    animal.php

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Animal extends CI_Controller {
    
        function __construct() {
            parent::__construct();
        }
    
    
        public function index(){
           // Stuff goes here
        }
    
        public function reroute($animal, $method) {
           //Check the $animal is valid
           //Check the $method is valid
           //Do stuff   
        }
    
    
    }
    
    /** End of file animal.php **/
    

    只需创建一个要从中继承的类

    class Animal extends CI_Controller {
       function __construct() {
            parent::__construct();
        }
    
        public function _remap($method, $params=array()){
           $this->animal = ucwords($this->router->fetch_class());
           $allowed_methods = array("Tame", "Buy", "Sell", "Gift");
           if (in_array($method, $allowed_methods)):
               // Model zoo has been autoloaded      
               data["foobar"] = $this->zoo->get_data($this->animal, $method);
               // Stuff goes here
           else:
              $this->index();
           endif;
        }
    }
    
    class Antelope extends Animal {
        public function index(){
           // Stuff goes here
        }
    }
    

    只需创建一个要从中继承的类

    class Animal extends CI_Controller {
       function __construct() {
            parent::__construct();
        }
    
        public function _remap($method, $params=array()){
           $this->animal = ucwords($this->router->fetch_class());
           $allowed_methods = array("Tame", "Buy", "Sell", "Gift");
           if (in_array($method, $allowed_methods)):
               // Model zoo has been autoloaded      
               data["foobar"] = $this->zoo->get_data($this->animal, $method);
               // Stuff goes here
           else:
              $this->index();
           endif;
        }
    }
    
    class Antelope extends Animal {
        public function index(){
           // Stuff goes here
        }
    }
    

    诀窍是认识到动物类型是可变的,并且您试图将其映射到静态文件不要这样做。只需将动物作为第一个参数传递给索引函数即可。这就是参数的作用:变量

    class Animal extends CI_Controller{
        function __construct(){
            parent::__construct();
        }
    
        function index($animal){
            echo "I'm the {$animal}!";
        }
    }
    
    并设置单一路线:

    $route['animal/(:any)]=“animal/index/$1”

    现在,如果您前往
    http://localhost/yourapp/animal/antelope

    CodeIgniter会回应“我是羚羊!”

    在评论后编辑: CodeIgniter在路由文件中从上到下运行,并在找到有效路由文件时中断。您可以将其放置在
    config/routes.php
    文件的底部

    $route['(:any)/buy/(:id)'] = "animal/$1/buy/$2";
    $route[':any'] = "animal/index/$1";
    //etc
    

    您需要重新路由所有其他控制器。

    诀窍是认识到动物类型是可变的,并且您正在尝试将其映射到静态文件不要这样做。只需将动物作为第一个参数传递给索引函数即可。这就是参数的作用:变量

    class Animal extends CI_Controller{
        function __construct(){
            parent::__construct();
        }
    
        function index($animal){
            echo "I'm the {$animal}!";
        }
    }
    
    并设置单一路线:

    $route['animal/(:any)]=“animal/index/$1”

    现在,如果您前往
    http://localhost/yourapp/animal/antelope

    CodeIgniter会回应“我是羚羊!”

    在评论后编辑: CodeIgniter在路由文件中从上到下运行,并在找到有效路由文件时中断。您可以将其放置在
    config/routes.php
    文件的底部

    $route['(:any)/buy/(:id)'] = "animal/$1/buy/$2";
    $route[':any'] = "animal/index/$1";
    //etc
    

    您需要重新路由以上所有其他控制器。

    为每种动物类型设置一个控制器似乎有点过分。你确定你不能用一个控制器来控制吗(在我看来你可以)。事实上,我不能。这是对我试图实现的目标的过于简单的描述。谢谢哎呀。我是说,是的,我想用一个控制器。但不,我不认为这个问题是一个小问题。为每种动物类型配备一个控制器似乎有点过分了。你确定你不能用一个控制器来控制吗(在我看来你可以)。事实上,我不能。这是对我试图实现的目标的过于简单的描述。谢谢哎呀。我是说,是的,我想用一个控制器。但是不,我不认为这个问题是微不足道的。谢谢你的回答,但真正的问题是“动物类型”是从数据库加载的,并且会随着时间的推移不断增加。我不想继续重访这个项目,为数据库中的新元素添加新类!。。。但是你有每种动物的档案吗?你自己也是这么说的??i、 例如,我使用数据库中的名称手动复制和粘贴antelope.php中的代码,以满足我的截止日期。然后我意识到这不是一种可持续的方式。随着项目的发展,我需要不断地重新访问它来创建新的控制器或类!谢谢你的回答,但真正的问题是“动物类型”是从数据库加载的,并且会随着时间的推移不断增加。我不想继续重访这个项目,为数据库中的新元素添加新类!。。。但是你有每种动物的档案吗?你自己也是这么说的??i、 例如,我使用数据库中的名称手动复制和粘贴antelope.php中的代码,以满足我的截止日期。然后我意识到这不是一种可持续的方式。随着项目的发展,我需要不断地重新访问它来创建新的控制器或类!谢谢你,雷斯。我想到了第一个选项,但我放弃了它,因为它确实需要许多几乎为空的控制器文件。我喜欢你给出的第二个解决方案,特别是因为我可以从数据库中加载动物列表,但有两个问题是:(1)URL需要像中一样干净,因为这是一个站点重新设计,会显示太多断开的链接。(2) 现场还有其他“与动物无关”的控制器。另外,您认为我可以使用上面的解决方案2,然后使用ApacheHTAccess来清理URL吗?这会影响性能吗?您可以使用.htaccess重新路由到动物控制器,但这只是一个问题