Error When Loading View in My_Controller - php

I have been trying to implement the solution referenced at:
Error when trying to load view in my_controller
Don't know why, but Aptana indicates a syntax error on my line:
parent::load->view('common/header_out');
...which is seconded by the interpreter when I execute: "unexpected T_OBJECT_OPERATOR"
EDIT: Previously I tried using:
$this->load->view('common/header_out');
...which generated the following errors:
Warning: include(application/errors/error_php.php) [function.include]: failed to open stream: No such file or >directory in /home/uom2/www/system/core/Exceptions.php on line 167
Warning: include() [function.include]: Failed opening 'application/errors/error_php.php' for inclusion >(include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear') in /home/uom2/www/system/core/Exceptions.php on >line 167
I am using CI 2.0.
Any help would be appreciated.
application/core/Uom_Controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Uom_Controller extends CI_Controller
{
public $data = array(); // Array to store data - passed to views.
protected $view_path = null; // Here to overide the view path if n
public function __construct()
{
parent::__construct();
}
protected function check_session()
{
//Here goes your function
}
protected function render() {
$data['page_title'] = 'Your title';
$data['page_title'] = 'Your title';
parent::load->view('common/header_out');
parent::load->->view('home/home', $data);
parent::load->->view('common/foot');
echo "view rendered: ".date('Y-m-d H:i:s');
}
}
controllers/home/home.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends Uom_Controller {
function __construct()
{
parent::__construct();
// Set the view path manually for this method.
$this->view_path = 'home/home.php';
}
function __destruct() {
$this->render();
}
public function index()
{
echo "home updated";
}
public function out()
{
echo "logout";
echo "<br>";
echo $this->view_path;
echo "<br>";
}
}
obviously the intention is similiar to the SO post at the top of this post: I want the $this->render(); to be called and render my page header, body, and footer.
(My next task is to add the authentication check to this MY_Controller pattern, so I am hoping that whaever solution works for calling views also works on models.
Thanks everyone and I really have spent hours trying to find a solution to this beforw once again turning to SO.

I think your problem is you are calling parent::load->view instead of $this->load->view

Ugh. here is the problem:
function __destruct() {
$this->render();
}
According to a remark by Phil Surgeon on here
__destruct does not work as expected due to the way everything is
inherited.
...and further down the page, a more detailed explanation that seems to make sense:
The problem is that the controller IS the CI superobject, once you
destroy that, you lose all access to CI. And you don’t have any
control over the destruct process, so you don’t know what is still
accessable, and what isn’t.
Using __destruct() for anything other than cleanup actions for
destruction of the current object is a very bad idea…
The short answer is that things work so long as you are not calling a method from the extended class from "_destruct".
Hopes this helps someone else.

Related

CodeIgniter: error loading custom library

I created a custom Library in CodeIgniter, and positioned it in application/libraries/VarMatrixSpecanimal.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class VarMatrixSpecanimal {
protected $variabiliMatrix;
public function __construct() {
$variabiliMatrix['cat']['no']=1;
$variabiliMatrix['dog']['a']=2;
$variabiliMatrix['bird']['b']=3;
}
public function get_matrix() {
return $this->variabiliMatrix;
}
}
?>
Then in one controller (application/controllers/certificate.php) method I added these two lines of code:
public function save1()
{
//..... some more code before
$this->load->library('VarMatrixSpecanimal');
$numerical_values = $this->varmatrixspecanimal->get_matrix();
//..... some more code after
But when I call save1 method, I get this error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Certificate::$varmatrixspecanimal
Filename: controllers/certificate.php
Line Number: 139
I don't understand where I do wrong, please help me.
I checked also CodeIgniter help http://www.codeigniter.com/user_guide/general/creating_libraries.html but I was not able to get my error
Add get_instance() function in constructor in VarMatrixSpecanimal.php
public function __construct()
{
$this->variabiliMatrix =& get_instance();
}
After this load library on your controller
$this->load->library('varmatrixspecanimal');
I have experienced anything like this because I forgot this line of code:
parent::__construct
in my __construct or constructor.
Add include file with correct path at the top of program will solve this issue.
include "../../VarMatrixSpecanimal.php";
Try to change your constructor from this:
public function __construct() {
$variabiliMatrix['cat']['no']=1;
$variabiliMatrix['dog']['a']=2;
$variabiliMatrix['bird']['b']=3;
}
to this:
public function __construct() {
$this->variabiliMatrix['cat']['no']=1;
$this->variabiliMatrix['dog']['a']=2;
$this->variabiliMatrix['bird']['b']=3;
}
Also, when loading your library i think you don't need to uppercase any letters

Passing parameter to controller always get "Page Not Found" - CodeIgniter

I try to pass a parameter to my controller functions from my views but I always get "Page Not Found".
I've been looking for any solutions possible for my problem from here, here, and here but I still couldn't find working solutions and I still don't know what could be the problem. I think I've set everything right, but it's not working. Please tell me if there's something I'm doing wrong.
Here is my controller: Controll.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Controll extends CI_Controller{
public function about($thing){
$case = "About Us";
$data['page'] = $case;
$data['p'] = $thing;
print($thing);
//$this->load->view('/header/header',$data);
$this->load->view('about_us',$data);
$this->load->view('/footer/footer');
}
public function mountain_destination($thing){
$data['page'] = "Mountain Destination";
$data['p'] = $thing;
//$this->load->view('/header/header',$data);
$this->load->view('mountain_destination',$data);
$this->load->view('/footer/footer');
}
}
?>
Here's my routes setting:
$route['(:any)'] = "controll/$1";
$route['default_controller'] = "controll";
$route['404_override'] = '';
And here's one of my script called by onClick that tries to call that controller function:
function goTab(thing,tab){
switch(thing){
case "about":
$body.load("<?php echo site_url('about'); ?>/"+$(tab).text().toLowerCase().replace(/ /g,''));
break;
case "mountain":
$body.load("<?php echo site_url('mountain_destination'); ?>/"+$(tab).text().toLowerCase().replace(/ /g,''));
break;
}
My script runs well and it produces right url. For example, I put parameter "myteam" into "goTab" function, it produces link like this:
"http://127.0.0.1/about/myteam". This link's supposed to call "about" function inside my controller and pass "myteam" as parameter. But instead it returns "Page Not Found". And when I try to call "about" function without any parameters like this: "http://127.0.0.1/about", I get missing argument error like this:
Severity: Warning
Message: Missing argument 1 for Controll::about()
Filename: controllers/Controll.php
Line Number: 22
Please help me.
Thanks in advance.
You're not passing it to your about method. You're looking for a method called myteam within your Controll controller.
Amend your Routes to this;
$route['about/(:any)'] = 'controll/about/$1';
$route['mountain_destination/(:any)'] = 'controll/mountain_destination/$1';
Hope this helps.
This answer of mine might also help you;
Codeigniter Menu from Database
You can create a variable and pass it within constructor and simply use that whenever you need as
class Controll extends CI_Controller {
private $thing;
public function __construct() {
parent::__construct();
$this->thing = $this->uri->segment(3);
}
public function about() {
$data['p'] = $this->thing;
echo $this->thing;
}
public function mountain_destination($thing) {
$data['p'] = $this->thing;
echo $this->thing;
}
}

__destruct called twice in CodeIgniter

First: I've already reviewed all the questions I can find about __destruct() and CodeIgniter, and none seem to address the same issue I'm seeing.
Right. That aside. I'll show code first, as the problem will make more sense after reading. (N.B.: some code redacted, but nothing important to the question. The Billing::index function however, is comprises only one space character in production as well as here)
application/core/MY_Controller.php
class MY_Controller extends CI_Controller{
public $view = '';
public $data = array();
public $template = '';
function __destruct(){
if(!is_null($this->template) && ($this->template == '')){
$this->template = 'public';
}
if($this->view == ''){
$this->view = $this->uri->segment(1,'index').'/'.$this->uri->segment(2,'index');
}
if(!is_null($this->template)){
echo $this->load->view('templates/'.$this->template.'/top',$this->data,true);
}
echo $this->load->view('views/'.$this->view,$this->data,true);
if(!is_null($this->template)){
echo $this->load->view('templates/'.$this->template.'/bottom',$this->data,true);
}
}
}
class MY_ProtectedController extends MY_Controller{
function __destruct(){
parent::__destruct();
}
}
application/controllers/billing.php
class Billing extends MY_ProtectedController{
public function index(){ }
}
This, loads perfectly. The main "billing" page is just HTML, so the destructor fires the right template and view.
However, I would love to remove the redundant public function index(){ }, as it really does nothing.
So if I remove that from application/controllers/billing.php so that it's just
class Billing extends MY_ProtectedController{ }
and visit /billing, I get the output I want, however, I get it twice.
If I do some test echos, MY_ProtectedController::__destruct() is called once, but MY_Controller::__destruct is called twice.
My question is: why is this the case, and can it be stopped?
I'm not super familiar with CI core, and I figure that by the time I dig far enough to find the controller instantiation, someone here has probably already got an answer. Will of course update this if my own digging turns anything up.
Solution ended up being fairly simple - but credit for spotting this goes to one of our other programmers.
If I put some code in my 404 route to prevent the auto-destructor from running, I only get one set of output.
class Index extends MY_PublicController {
public function not_found(){
$this->cancel_destruct = TRUE;
}
}
class MY_Controller extends CI_Controller{
$this->cancel_destruct = FALSE;
function __destruct(){
if(!$this->cancel_destruct){
[...]
}
}
}

CodeIgniter : core/ override, missing load/loader class

Okay, i am having this issue and google is not helping and i think it is because my issue is too simple or something.
I'm trying to load whatever or connect with load->database(); but it aint working.
Here is my code with comments, it will explain everything.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CI_Lang {
function __construct() {
# Show all files included
print_r(get_included_files()); // There is nothing like loader or load .php
# Check for loader file
if( ! class_exists('loader') || ! class_exists('load')) {
echo 'no load '; // is being shown,
}
# Check Database connection before
if (isset($this->db->conn_id) && is_resource($this->db->conn_id)) {
echo 'database is loaded and conected ';
} else {
echo 'no connection '; // is being shown,
}
# Get Database connection
$this->load->library('database');
/*
Script dies on line above with error:
Fatal error: Call to a member function library() on a non-object in
application/core/Lang.php on line 38
*/
$this->load->database();
# Check Database connection after
if (isset($this->db->conn_id) && is_resource($this->db->conn_id)) {
echo 'database is loaded and conected ';
} else {
echo 'no connection ';
}
# Register as loaded
log_message('debug', 'JK_Lang Class initialized');
}
}
This script is named Lang.php and it is in application/core folder. It is supposed to override the main Lang class and it is. The problem is, it seems to be all alone. How can i include other methods here?
I already have modified autoload.php and set there:
$autoload['libraries'] = array(
# We need Loader
'loader',
# Connect with DB
'database',
# Session Start
'session'
);
And now i have ran out of ideas, any help please ?
The problem was, i had no codeigniter instance declared. So there were 2 methods to fix it.
First, i could make a function like:
function __construct() {
$this->run();
}
function run() {
/* All my __construct code here */
}
Or as i actually fixed my problem, i made a library and to get all things needed to work with codeigniter i made an instance:
class L {
var $CI;
function __construct() {
$this->CI =& get_instance();
$this->CI->load->database();
$this->test();
}
function test() {
$query = $this->CI->db->query("SELECT * FROM `stackoverflow.com`");
}
}

Yii: Unexpected invocation of CHttpSession::close when running a method in custom class

strange problem here. Using Yii framework I have the following class
class HtmlTableUi
{
public function __construct(CWebModule &$module,$data,$actions)
{
//...code goes here...
}
protected function renderTable()
{
//... code goes here ...
}
}
I can call HtmlTableUi::renderTable() from instance of HtmlTableUi in SchedulerModule class which is the main class in a separate application module. My SchedulerModule.php file:
<?php
Yii::import('scheduler.components.HtmlTableUi');
class SchedulerModule extends CWebModule
{
public function init()
{
parent::init();
}
public function beforeControllerAction($controller, $action)
{
return true;
}
public function printUI($data,$actions,$submitPath)
{
$ui = new HtmlTableUi($this,$data,$actions,$submitPath);
$ui->renderTable();
}
}
Here comes the tricky part - when I call SchudulerModule::printUI from view (index.php) this way
<?php
$this->module->printUI($casino,$actions,null);
?>
the code flow goes through SchudulerModule::printUI until reaching the point where renderTable is invoked ($ui->renderTable();) and instead stepping at the first line inside the body of that method it, contrary to any logic and rules, surprisingly jumps to CHttpSession::close !!?
Notice the Call Stack before invoking renderTable
protected/modules/scheduler/SchedulerModule.php.SchedulerModule->printUI:153
protected/modules/scheduler/views/default/index.php.require:21
/home/default/workspace/src/vlt/web/yii-1.1.12.b600af/framework/web/CBaseController.php.CBaseController->renderInternal:127
/home/default/workspace/src/vlt/web/yii-1.1.12.b600af/framework/web/CBaseController.php.CBaseController->renderFile:96
/home/default/workspace/src/vlt/web/yii-1.1.12.b600af/framework/web/CController.php.CController->renderPartial:870
/home/default/workspace/src/vlt/web/yii-1.1.12.b600af/framework/web/CController.php.CController->render:783
protected/modules/scheduler/controllers/DefaultController.php.DefaultController->actionIndex:57
and after:
/home/default/workspace/src/vlt/web/yii-1.1.12.b600af/framework/web/CHttpSession.php.CHttpSession->close:134
/home/default/workspace/src/vlt/web/yii-1.1.12.b600af/framework/web/CHttpSession.php.SchedulerModule->printUI:0
protected/modules/scheduler/views/default/index.php.require:21
/home/default/workspace/src/vlt/web/yii-1.1.12.b600af/framework/web/CBaseController.php.CBaseController->renderInternal:127
/home/default/workspace/src/vlt/web/yii-1.1.12.b600af/framework/web/CBaseController.php.CBaseController->renderFile:96
/home/default/workspace/src/vlt/web/yii-1.1.12.b600af/framework/web/CController.php.CController->renderPartial:870
/home/default/workspace/src/vlt/web/yii-1.1.12.b600af/framework/web/CController.php.CController->render:783
protected/modules/scheduler/controllers/DefaultController.php.DefaultController->actionIndex:57
Has anyone had similar issue? Can anyone explain that behavior?
I found the solution!!! The access modifier of renderTable is supposed to be public, not protected. This wrong somehow in Yii framework results in unexpected runtime behavior instead of compilation error.

Categories