简体   繁体   English

将组织的/name 重定向到调用Home.php controller 的组织的/home/name

[英]Redirect /nameoftheorganization to /home/nameoftheorganization which calls Home.php controller

I am using php codeigniter on a project that has several organizations.我在一个有多个组织的项目中使用 php codeigniter。 Each organization will have its own website, I need to redirect each user to its organization website according to the organization name.每个组织都有自己的网站,我需要根据组织名称将每个用户重定向到其组织网站。

I got this partially running.我得到了这个部分运行。 Using localhost as example:以本地主机为例:

localhost/application/ -> Goes to the correct view; localhost/application/ -> 转到正确的视图;

localhost/home/nameoftheorganization -> Goes to the correct organization view; localhost/home/nameoftheorganization -> 转到正确的组织视图;

But is there a way to eliminate the "Home" controller?但是有没有办法消除“家”controller?

My routes.php looks like this:我的 routes.php 看起来像这样:

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

My Home.php controller looks like this:我的 Home.php controller 看起来像这样:

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 looks like this htaccess 看起来像这样

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

But is there a way to eliminate the "Home" controller?但是有没有办法消除“家”controller?

this, in your example, can be achieved with the following route:在您的示例中,这可以通过以下路线实现:

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

this will feed your home() function of the Home.php controller.这将为您的 Home.php controller 的home() function 提供食物。

above that route you may want to add routes to get to any other function within that Home.php controller在该路线之上,您可能需要添加路线以到达该 Home.php controller 内的任何其他 function

don't forget about the three reserved routes, mainly:不要忘记保留的三个路线,主要是:

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

so a complete route for your example could look like:因此,您的示例的完整路线可能如下所示:

$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;

now you can type www.yoursite.com/organization1 , which will be sent to controller Home.php , with the $id='organization1'现在您可以输入www.yoursite.com/organization1 ,它将被发送到 controller Home.php$id='organization1'

note: You'll need to filter out requests which do not mach your database of organizations within your home() function and send them to your custom 404 page.注意:您需要过滤掉与 home() function 中的组织数据库不匹配的请求,并将它们发送到您的自定义 404 页面。

more about Codeigniter Routing更多关于Codeigniter 路由

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM