【问题标题】:Redirect /nameoftheorganization to /home/nameoftheorganization which calls Home.php controller重定向 /nameoftheorganization 到 /home/nameoftheorganization 调用 Home.php 控制器
【发布时间】:2020-07-16 03:54:41
【问题描述】:

我在一个有多个组织的项目上使用 php codeigniter。每个组织都有自己的网站,我需要根据组织名称将每个用户重定向到其组织网站。

我让这个部分运行。以 localhost 为例:

localhost/application/ -> 转到正确的视图;

localhost/home/nameoftheorganization -> 转到正确的组织视图;

但是有没有办法消除“Home”控制器?

我的 routes.php 看起来像这样:

$route['default_controller'] = 'home';
$route['404_override'] = 'home/page_not_found';
$route['translate_uri_dashes'] = FALSE; 

我的 Home.php 控制器如下所示:

public function __construct()
{
    parent::__construct();
    // Your own constructor code
    $this->load->database();
    $this->load->library('session');
    // $this->load->library('stripe');
    /*cache control*/
    $this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
    $this->output->set_header('Pragma: no-cache');
    if (!$this->session->userdata('cart_items')) {
        $this->session->set_userdata('cart_items', array());
    }
}

public function index() {
    $this->home();
}

public function home($domain = '') {
    if($domain != ''){
        $frontend = $this->crud_model->get_frontend_information($domain);
    }else{
       $frontend = $this->crud_model->get_frontend_information('myapp'); 
    }

    foreach($frontend AS $arr){
        if(is_array($arr)){
            $website[$arr['key']] = $arr['value'];
            }
        }

    $this->session->set_userdata('website', $website);


    $page_data['page_name'] = "home";
    $page_data['page_title'] = get_phrase('home');
    $this->load->view('frontend/'.get_frontend_settings('theme').'/index', $page_data);
}

htaccess 看起来像这样

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

【问题讨论】:

    标签: php .htaccess codeigniter


    【解决方案1】:

    但是有没有办法消除“Home”控制器?

    在您的示例中,这可以通过以下途径实现:

    $route['(:any)'] = 'home/home/$1';
    

    这将提供 Home.php 控制器的 home() 函数。

    以上您可能想要添加路由以访问该 Home.php 控制器中的任何其他功能

    不要忘记预留的三个路线,主要是:

    $route['default_controller'] = 'yourdefaultcontroller';

    因此,您的示例的完整路线可能如下所示:

    $route['default_controller'] = 'home';
    $route['about']='home/about';
    $route['contact']='home/contact';
    $route['(:any)'] = 'home/home/$1';
    $route['404_override'] = 'sitemanager/my_404';
    $route['translate_uri_dashes'] = FALSE;
    

    现在您可以键入 www.yoursite.com/organization1,它将发送到控制器 Home.php,并带有 $id='organization1'

    注意:您需要在 home() 函数中过滤掉与您的组织数据库不匹配的请求,并将它们发送到您的自定义 404 页面。

    更多关于Codeigniter Routing

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多