Using a controller on 2 pages

edited December 2012 in Share
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 page works perfectly and now I want the same content working on 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! :)


  • edited 2:27AM
    If you are using a controller for your contact page and not using Fuel_pages to render the page, then you will need to create another controller specific to the mobile version or create a route to that same file and then perhaps use the URI segment values to change the layout.

    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();
  • edited 2:27AM
    How does the controller know you're looking at it if you're pointing at mobile/contact instead of just contact which Fuel would look for the controller first normally.
    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.
  • edited 2:27AM
    I recently had the same issue, most of my stuff is responsive. For this project I needed a mobile only approach (JQM).

    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.
  • edited 2:27AM
    You would need to use a route to have 2 different URIs pointing to the same controller. Then in the controller, you could decipher which one has the "mobile" segment and change the layout variable accordingly.
  • edited 2:27AM
    Cool answers... but I found my own solution while working with another CMS (had to whip up a quick site). They used layouts such as Fuel, but for some reason I didn't think of working with the layouts until I was working on this site. So what I ended up doing was creating two layouts in _layouts. one called full.php and one mobile.php. Then detecting 1st) GET parameter 2nd) Session 3rd) Cookie 4th) device type. Here's the code I used. For device detection I use the very nice Mobile_Detect class which I added to the helper file. (I can't believe how long it took me to come up with this, I feel embarrassed, but hopefully it can help someone else trying to do something like this)

    <?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 ?>
  • edited 2:27AM
    Good solution. Thanks for the post back.
Sign In or Register to comment.