fefe
fefe

Reputation: 9065

Codeigniter views in main view

I'm loading views in a main view from controller with codeigniter like this

Controller

public function product_modules($domain_id)
{           
    $this->load->model('/admin/Model_products', '', TRUE);      
    $data['product_boxes'] = $this->Model_products->getProducBoxes($domain_id);
    $this->load->view('admin/dashboard',$data, null, true);

}

Main View

 $this->view($_SERVER['REQUEST_URI']);

but if the requested uri is containing query strings, the view is not getting loaded and I get a type Unable to load the requested file: /admin/product_modules/1.php. What would be the best workaround to call views dynamically?

Upvotes: 1

Views: 301

Answers (1)

Madan Sapkota
Madan Sapkota

Reputation: 26111

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Example extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    public function _remap($method, $params = array())
    {
        // dynamically assign the method with parameters support
        $this->view($method, $params);
    }

    public function product_modules($domain_id)
    {
        $this->load->model('/admin/Model_products', '', TRUE);
        $data['product_boxes'] = $this->Model_products->getProducBoxes($domain_id);
        $this->load->view('admin/dashboard', $data, null, true);
    }

    public function view($method, $param1 = '', $param2 = '')
    {
        // params you can sent to models
        $data['users'] = $this->model_name->get_user($param1);

        // or views
        $data['myvar'] = $param2;

        // and load the view dynamically
        $this->load->view($method, $data);
    }

}

So

if the URL is http://example/controller/method then the above will search for method.php view file,

if URL is http://example/controller/product_modules/1 then it search for product_modules.php view file.

Hope this helps. Thanks!!

Upvotes: 1

Related Questions