在PHP Codeigniter中有动态控制器吗

n1bvdmb6  于 2023-03-06  发布在  PHP
关注(0)|答案(2)|浏览(123)

我是新的PHP编码,我需要知道是有无论如何有一个动态控制器名称(而不是控制器函数名称)在Codeigniter。
我有大约5个控制器,如Sf_client. php,Sf_network. php,Sf_display. php等...和编码是相同的,在所有这5个控制器,除了一个变量名是不同的。

<?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    class Sf_client extends UTF_Controller
    {

        function __construct() {
            parent::__construct();
            $this->resource_subtype = "client";
        }

        public function list() {
            $data['page_title'] = 'Client list';
            $this->template->set_title('CTL - '.$data['page_title']);
            $this->template->build('resources/list',$data);
        }
    }
?>

除了$this->resource_subtype = "client"之外,其他控制器与上面类似,它将像“网络”,“显示”等。
现在的网址就像,http://localhost/Sf_client/http://localhost/Sf_network/等...
是否有一个控制器文件并传递变量名?这样我就可以消除重复的控制器文件。
我有其他控制器以及。所以,我需要的东西一样,如果它匹配的网址“sf”。
任何帮助都将不胜感激。谢谢。

sg3maiej

sg3maiej1#

创建动态controllers是不可能的,因为即使你动态命名class,你仍然需要一个同名的文件,这就是CIURL的工作原理。

不过,您可以配置路由,以便当您命中某个URL时,它对应于另一个controller,然后在该controller中,您可以检查URL,并相应地为变量分配所需的值。

我为你的问题做了一个演示,这个↓对我很有效。看看,它对你是否也有效。

路径.php

$route['default']    = 'Sf_client'; // say default is 'Sf_client' -- you can ignore it if you already have a default. We'll be creating only this controller.
$route['Sf_network'] = 'Sf_client'; // route requests to this controller
$route['Sf_display'] = 'Sf_client'; // ...

查看

<a href="<?PHP echo base_url(); ?>">Sf_client</a> <!-- OR base_url('Sf_client'); // if not default -->
<a href="<?PHP echo base_url('Sf_network'); ?>">Sf_network</a> <!-- will correspond to Sf_client but the URL won't change -->
<a href="<?PHP echo base_url('Sf_display'); ?>">Sf_display</a> <!-- ... -->

Sf_客户端.php(控制器)

class Sf_client extends UTF_Controller{

    function __construct() {

        parent::__construct();

        $segment = $this->uri->segment(1); // get the first segment from the URL which is controller's name

        switch($segment){ // check the segment value

            case 'Sf_network': 
                $this->resource_subtype = 'network';
                break;

            case 'Sf_display': 
                $this->resource_subtype = 'display';
                break;

            // ignore if default ↓↓
            /* case 'Sf_client': 
                $this->resource_subtype = 'client';
                break; */

            default: 
                $this->resource_subtype = 'client';
        }
        echo $this->resource_subtype; // will return network if in http://localhost/Sf_network 
    }
4dbbbstv

4dbbbstv2#

你可以这样做

public function mytest($controller = 'home', $method = 'index', $slug = false)
{
    //uri(3) -> controller
    //uri(4) -> method
    //uri(5) -> slug
    $begin_uri_count = 3;
    
    $controller = (is_null($this->uri->segment($begin_uri_count))?$controller:$this->uri->segment($begin_uri_count));
    $method = (is_null($this->uri->segment($begin_uri_count+1))?$method:$this->uri->segment($begin_uri_count+1));
    $slug = $this->uri->segment($begin_uri_count+2);
    
    if( !$slug OR is_null($slug) )
    {
        $slug = 'overview';
    }
    
    echo 'C = '.$controller.' / M = '.$method.' / S = '.$slug;
}

然后,如果愿意,您可以通过白名单检查正确的控制器/方法名称。

相关问题