Redirect /nameoftheorganization to /home/nameoftheorganization which calls Home.php controller

Viewed 34

I am using php codeigniter on a project that has several organizations. 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/home/nameoftheorganization -> Goes to the correct organization view;

But is there a way to eliminate the "Home" controller?

My routes.php looks like this:

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

My Home.php controller looks like this:

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

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

But is there a way to eliminate the "Home" 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.

above that route you may want to add routes to get to any other function within that Home.php controller

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'

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.

more about Codeigniter Routing

Related