Using a controller on 2 pages
Hi again,
So, I tried finding a way to do this simply, but I'm at a loss (though I think it might have something to do with view _variables.
I have a controller for my contact page. My CMS has the form and content that I'm wanting to reuse.
my site.com/contact page works perfectly and now I want the same content working on site.com/mobile/contact but with a different view layout (mobile of course).
What's the best way to achieve this?
I should note that currently I'm pulling the data from each of the root pages via $this->db->get_where to grab the content out of the database directly instead of using the module for Fuel's Pages module as I'm unsure how to access that directly either... again possibly something to do with the view _variables?
Thanks!
Comments
I think what I would recommend is using Fuel_pages to render the page and you could pass your variables to the view using the add_variables method. Then in your view, or your controller (which you may need a route for still), you could change the layout based on the URI segment like so:
if (uri_segment(1) == 'mobile'))[ { $vars['layout'] = 'mobile'; } // 1.0 syntax $this->fuel->pages->render('contact', $vars); // 0.93 syntax $page_init = array('location' => 'contact'); $this->load->module_library(FUEL_FOLDER, 'fuel_page', $page_init); $this->fuel_page->add_variables($vars); $this->fuel_page->render();
Do I need to create /mobile/contact.php in the controllers folder first?
Also, is there another way to access the Pages modules of Fuel through the model instead of directly accessing through db?
Perhaps I should instead ask, how would you create a separate mobile site if you're not creating a responsive design?
Thanks again.
I used a separate instance of FUEL under a subdomain and used the DSN property of the models to connect to the 'production' database and get just the data I needed. For this project is made more sense to do it that way then mess up the production sites code base.
If you didn't want to do it by changing layouts as admin suggests, you could capture the same thing in the controller and either pass it to a separate controller method or do whatever you need to there.
<?php /* All about mobile detection */ $fullSite = FALSE; $mobileSite = FALSE; $mobile = FALSE; $was = ''; // choose mobile... or $fullSite $this->uri->init_get_params();// check for $_GET params & set $fullSite if ?full is detected. if ($this->input->get() && array_key_exists('full',$this->input->get())) $fullSite = TRUE; if ($this->input->get() && array_key_exists('mobile',$this->input->get())) $mobileSite = TRUE; $domain = $_SERVER['HTTP_HOST']; $detect = new Mobile_Detect; // detect mobile to go straight to mobile $deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer'); if (!$fullSite == '' || !$mobileSite == '') { // check URL flag if ($fullSite) $mobile = FALSE; if ($mobileSite) $mobile = TRUE; } else if ($this->session->userdata('which')) { // check session 2nd if ($this->session->userdata('which') == 'full') $mobile = FALSE; if ($this->session->userdata('which') == 'mobile') $mobile = TRUE; } else if (get_cookie('which')) { // check cookie 3rd if (get_cookie('which') == 'full') $mobile = FALSE; if (get_cookie('which') == 'mobile') $mobile = TRUE; } else if ($deviceType == 'phone') $mobile = TRUE; // if mobile device detected as 4th option else $mobile = FALSE; // anything else gets full site. if ($mobile) { // if we determined we need to view mobile site $this->session->set_userdata('which','mobile'); set_cookie(array('name' => 'which', 'value' => 'mobile', 'domain' => '.'.$domain, 'expire' => 604800)); } else { // if we determined we should show full site $this->session->set_userdata('which','full'); set_cookie(array('name' => 'which', 'value' => 'full', 'domain' => '.'.$domain, 'expire' => 604800)); } /* End mobile detection */ ?> <?php // display mobile site view! // if ($mobile) $this->load->view('_layouts/mobile'); // load mobile view file else $this->load->view('_layouts/full'); // load full page view file ?>