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?
<?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!!
Related
My template parser looks like this (p/s the .'/'. is for readability):
$this->parser->parse($this->settings['theme'].'/'.'header', $data);
$this->parser->parse($this->settings['theme'].'/'.'register', $data);
$this->parser->parse($this->settings['theme'].'/'.'footer', $data);
I don't want to declare $this->parser->parse($this->settings['theme'].'/'.'header', $data); and $this->parser->parse($this->settings['theme'].'/'.'footer', $data); every time in my controller's functions.
How can I extend the MY_Parser.php so that I could use it like this instead:
$this->parser->parse($this->settings['theme'].'/'.'register', $data); will include the register.php between my header.php and footer.php automatically.
The benefit of doing this is to save 2 lines and if I have 20 functions, I can save 40 lines.
Just create a function (can be a helper, library extension or model):
function tpl($view, $data) {
$this->parser->parse($this->settings['theme'].'/'.'header', $data);
$this->parser->parse($this->settings['theme'].'/'.$view, $data);
$this->parser->parse($this->settings['theme'].'/'.'footer', $data);
}
If you want you can extend Parser and make a MY_Parser in the libraries folder and do:
class MY_Parser extends CI_Parser {
function tpl($view, $data) {
$this->parse($this->settings['theme'].'/'.'header', $data);
$this->parse($this->settings['theme'].'/'.$view, $data);
$this->parse($this->settings['theme'].'/'.'footer', $data);
}
}
Usage:
$this->parser->tpl($view, $data);
You could do this using $this->parser->parse() but that would require more code as you overwriting the default method and it's just as easy to introduce a new method.
UPDATE:
Using the MY_Parser method you might have to access $this->settings via $this->CI->settings thereby referencing the CI instance in CI_Parser depending on where this variable is coming from.
Create class with the name of your prefix class name in application/core folder and follow below code. $this->input->is_ajax_request() will only load view other then header and footer if request is from ajax. and in each controller you need to extend YOUR-PREFIX_Controller instead of CI_Controller
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class YOUR-PREFIX_Controller extends CI_Controller {
protected $header_data;
protected $footer_data;
protected $header_view;
protected $footer_view;
public function __construct() {
parent::__construct();
$this->header_view = 'path-to-header';
$this->footer_view = 'path-to-footer';
}
public function _output($output) {
if ($this->input->is_ajax_request()) {
echo ($output);
} else {
echo $this->load->view($this->header_view, $this->header_data, true);
echo ($output);
echo $this->load->view($this->footer_view, $this->footer_data, true);
}
}
}
?>
I have a custom library and need to pass some variables under specific method to my controller, then views.
Library code
Class Data {
function dviews($sitedata) {
$site1 = "www.site1.com";
$site2 = "site2.com";
}
}
My controller file
class Webs extends CI_Controller {
public function index()
{
$this->load->library('Data');
$data = $this->Data->dviews();
$this->load->view('pages/websites', $data);
}
}
and I pass the $data array using print_r on views.
is that correct or there is another way to get the list of these vars from library file and pass them?
You are setting variable inside method but returning nothing, so
Modify
Class Data {
function dviews($sitedata) {
$site1 = "www.site1.com";
$site2 = "site2.com";
}
}
To
Class Data {
function dviews($sitedata) {
return array( 'site1' => "www.site1.com", 'site2' => "site2.com");
}
}
Controller
class Webs extends CI_Controller {
public function index()
{
$this->load->library('Data');
$data = $this->Data->dviews();
// now print_r($data); will have array which you returned
$this->load->view('pages/websites', $data);
}
}
and in your view, pages/websites.php you can access like below
<?php echo $site1; ?>
<?php echo $site2; ?>
I'm with a doubt at this post:
http://www.ahowto.net/php/easily-integrateload-phpexcel-into-codeigniter-framework/
I've done up to libraries part (Excel.php).
But in the tutorial, where it starts Example Usage, where exactly I need to put all that code? In a new controller? Here in my project I tried to create a new Controller called Report. In report I've this code:
public function readReport() {
$this->load->library('excel');
$this->excel=PHPExcel_IOFactory::load(APPPATH."/third_party/teste.xlsx");
$this->excel->setActiveSheetIndex(0);
//get some value from a cell
$number_value= $this->excel->getActiveSheet()->getCell('C1')->getValue();
$data['header'] = $number_value;
$this->load->view('pages/home', $data);
}
But I have also a Pages controller to control the pages of Views, and when I try to output something of PHP Excel is not possible. In my Pages.php I've wrote: $data['header'] = $number_value; and in view . But the variable "number_value" is not in Pages.php because it's only at Report.php. How can I do to output the excel data at my home.php (view) correctly?
Here is my pages.php controller
class Pages extends CI_Controller {
public function view ($page = 'home') {
if (!file_exists(APPPATH.'views/pages/'.$page.'.php')) {
show_404();
}
$data['title'] = str_replace("_", " ", $page);
$data['header'] = $number_value;
$this->load->helper('url');
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
I'm not very familiar with PHPExcel but here are some thoughts.
First, to use a library in a Controller it must be loaded in that controller. You cannot access one controller from another. You could duplicate the code from Report in Pages but that seems a waste. You need reuseable code.
One "reuseable" approach is to create a model that uses the excel library. This will make it easy to reuse PHPExcel code just by loading the model in any controller.
A model version including a readReport() function might look like this.
class excel_model extends CI_Model
{
protected $excel;
function __construct()
{
parent::__construct();
$this->load->library('excel');
}
function readReport()
{
$this->excel = PHPExcel_IOFactory::load(APPPATH."/third_party/teste.xlsx");
$this->excel->setActiveSheetIndex(0);
//get some value from a cell
return $this->excel->getActiveSheet()->getCell('C1')->getValue();
}
}
Pages controller should be modified as follows.
class Pages extends CI_Controller
{
public function view($page = 'home')
{
//The following check isn't needed, codeigniter will do this automatically
//if (!file_exists(APPPATH.'views/pages/'.$page.'.php')) {
//show_404();
//}
$this->load->model('excel_model');
$this->load->helper('url');
$data['title'] = str_replace("_", " ", $page);
$data['header'] = $this->excel_model->readReport();
$this->load->view('templates/header', $data);
//You don't have to keep sending $data to the views because
//any variables loaded by the first call to load->view()
//will be visible the all other views loaded in this function.
$this->load->view('pages/'.$page);
$this->load->view('templates/footer');
}
}
I am new in CI.
I want to change function name in addressbar url with add_car to addcar.
Actually my url is created as below
http://localhost/projectName/controller/add_car
But I want following in URL
http://localhost/projectName/controller/addcar
Is it possible? Please help me.
[Note] : My actual method name is add_car.
You can do it by two methods
Method 01
Edit - config/routes.php
$route['controller/addcar'] = 'controller/add_car';
$route['controller/deletecar'] = 'controller/delete_car';
output - www.exapmle.com/controller/addcar
Method 02
change your controller function name as you like.
public function addcar($value='')
{
# code...
}
public function deletecar($value='')
{
# code...
}
Output -www.exapmle.com/controller/addcar
Further Knowledge
If you use $route['addcar'] = 'controller/add_car'; URL looks like
www.exapmle.com/addcar
Change add_car function to addcar in your controller
function add_car(){
//...
}
To
function addcar(){
^
//...
}
Or in routes.php
$route['controller/add_car'] = "controller/addcar";
$route['controller/([a-z]+)_([a-z]+)'] = "controller/$1$2";
Above example will route every requested action containing '_' between two strings to action/method without the '_'.
More about Code Igniter regular expression routes:
https://ellislab.com/codeigniter/user-guide/general/routing.html
You can use this on your route:
$route['addcar'] = 'Add_car/index';
$route['addcar/(:any)'] = 'Add_car/car_lookup/$1';
and your controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Add_car extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function car_lookup($method = NULL)
{
if (method_exists($this, $method))
{
$this->$method();
}
else
{
$this->index(); // call default index
}
}
public function index()
{
echo "index";
}
public function method_a()
{
echo "aaaaa";
}
public function method_b()
{
echo "bbbbb";
}
}
I have my controller
class Page extends CI_Controller {
public function id() {
$this->load->model('content');
$page = $this->uri->segment(3, 0);
if($page == 0)
$page = $this->content->get_default_page($page);
$data['navigation'] = $this->content->getNav();
$data['pagename'] = $this->content->get_pagename($page);
$data['content'] = $this->content->get_content($page);
$this->load->view('main', $data);
}
}
Now I'll try to explain.
Im getting navigation, and navigation text from mysql (id, navName, navText).
then im returning those elements in views/main_view.php in url like: http://abc.com/page/id/1 etc...
Now i need to create other controller like mySuperDuperModule which have some functions not just text.
The problem is that if i create new controller like Gallery(), i need to copy all the stuff from Page() controller to make website show the same.
Is there any way not to do that ?
You can create base controller under /application/core/MY_Controller.php
Here MY_ is the value specified for $config['subclass_prefix'] in /application/config/config.php
class MY_Controller extends CI_Controller
{
protected $data;
public function __construct()
{
parent::__construct();
$this->data = array();
}
public function loadPage($page)
{
$this->load->model('content');
if($page == 0)
$page = $this->content->get_default_page(); // I hope this function will give default page from database table. (A Change from your code)
$this->data['navigation'] = $this->content->getNav();
$this->data['pagename'] = $this->content->get_pagename($page);
$this->data['content'] = $this->content->get_content($page);
}
}
Your modified Page Class in /application/controllers/page.php
class Page extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function id()
{
$page = $this->uri->segment(3, 0);
parent::loadPage($page);
$this->load->view('main', $this->data);
}
}
and your new gallery controller can be in /application/controllers/gallery.php
class Gallery extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function id()
{
$page = 10; // Assuming gallery page index is 10 in database table OR you can change any logic here.
parent::loadPage($page);
$this->load->view('main', $this->data);
}
}
you can create as many controllers as you want by extending MY_Controller.
You can pass these two lines to any controller
and they will display the same template.
$data['navigation'] = $this->content->getNav();
$data['pagename'] = $this->content->get_pagename($page);
If you still dont understand try to use this library
https://github.com/philsturgeon/codeigniter-template