gpt4 book ai didi

php - 将/nameoftheorganization 重定向到调用 Home.php Controller 的/home/nameoftheorganization

转载 作者:行者123 更新时间:2023-12-04 10:17:14 27 4
gpt4 key购买 nike

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

我得到了部分运行。以本地主机为例:

localhost/application/-> 转到正确的 View ;

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

但是有没有办法消除“家庭” Controller ?

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

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

我的 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 看起来像这样
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?



在您的示例中,这可以通过以下路线实现:
$route['(:any)'] = 'home/home/$1';

这将满足您的 home() Home.php Controller 的功能。

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

不要忘记三个保留的路由,主要是:
$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,它将被发送到 Controller Home.php , 与 $id='organization1'
注意:您需要过滤掉与您的 home() 函数中的组织数据库不匹配的请求,并将它们发送到您的自定义 404 页面。

更多关于 Codeigniter Routing

关于php - 将/nameoftheorganization 重定向到调用 Home.php Controller 的/home/nameoftheorganization,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61021034/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com