Namespace not found on extending class - php

I have a problem with namespaces. Follow the code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class NavBar extends fwportal\controllers\template\NavBar {
function __construct()
{
var_dump('navBarPortal');
parent::__construct();
}
}
And the main class:
<?php
namespace fwportal\controllers\template;
use fwportal\controllers\NavbarPermissoes;
if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
Abstract class NavBar extends \CI_Controller
{}
This return the error below:
Fatal error: Class 'fwportal\controllers\template\NavBar' not found in /var/www/portalsibe/sistema/controllers/template/NavBar.php on line 6
Anyone can help me with this?
I don't know why this error occur, because I used in other files with the same mode and works ok.

If you are using Codeigniter 3 then most probably you can not extend "\CI_Controller" while you are defining namespace on a class.
May be this is the reason of getting error.

Related

PHP CodeIgniter models with the same name

I need your help, please!
I have 2 models with the same name
1.... application/admin/user/models/User_model.php, with this code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends CI_Model
?>
2.... application/front/user/models/User_model.php, with this code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends CI_Model
?>
Both classes without differences.
I need to use application/admin/user/models/User_model.php
from application/front/user/controllers/User.php
I tried to do the following changes:
If I add in application/front/user/models/User_model.php the namespace like this code:
<?php namespace customers;
defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends \CI_Model
<?
And from application/front/user/controllers/User.php, I call application/admin/user/models/User_model.php always gives me an error as application/front/user/models/User_model.php was not loaded.
If I delete the namespace from application/front/user/models/User_model.php my code always goes to this mode NEVER to admin/user/user_model.
I need to AVOID change admin/user/User_model.php because a lot of classes uses this MODEL.I only can change front/user/User_model.php
But I need to USE admin/user/user_model.
I call application/admin/user/models/User_model.php like this:
$this->load->model('user_model');
I need to load application/admin/user/models/User_model.php
Please help me!!!!
Thx!
Ani
That is simply not possible, since Codeigniter doesn't support Namespaces
Usually you would see an error like
Fatal error: Cannot declare class `User_model`, because the name is already in use
But Codeigniter prevents this, because it doesn't load any models which are already loaded. You can see this here.
So the only way out of your misery is to simply rename your models - even if you don't want to ;)
Hope this helps you
You can created subdirectory in models like this :
application/models/admin/User_model.php
application/models/user/User_model2.php
Load the model like this:
$this->load->model('admin/User_model');
$this->load->model('user/User_model2');
you should assigned your model to a different object name you can specify it via the second parameter of the loading method:
Use them like this :
$this->load->model('admin/User_model', 'admin_model');
$this->admin_model->some_method();
$this->load->model('user/User_model2', 'user_model');
$this->user_model->some_method();
For more : https://codeigniter.com/user_guide/general/models.html

In Codeigniter extends one controller into another controller

I am using codeigniter(3.1.5) and Ihave two controllers in my application/controllers/ folder. with name controller A and Controller B. I want to extends Controller A in Controller B so that I can use methods of Controller A. But it generates class not found error.
Here is my sample code:
A_Controller.php:
defined('BASEPATH') OR exit('No direct script access allowed');
class A_Controller extends CI_Controller {
public function index()
{
}
public function display(){
echo 'base controller function called.';
}
}
B_Controller.php:
defined('BASEPATH') OR exit('No direct script access allowed');
class B_Controller extends A_Controller {
}
I want to execute display() method of controller A in controller B.
If i put controller A in application/core/ folder and in application/config/config.php file make
$config['subclass_prefix'] = 'A_';
then I can able to access methods of controller A.
Please suggest.
Thanks in advance.
Extending a controller in another controller is not really good. Building with MVC and especially with CI, you have other options to achieve this.
Use a class MY_Controller inside application/core that extends the CI_Controller. Later, all (or some) of your controllers should extend the MY_Controller. In MY_Controller you can have many functions and you can call which function you want in your controller.
Use a library. Write your own library in application/libraries and load it in your controller wherever you want. A library is a class with functionality for your project.
Use a helper. Write your own helper in application/helpers and load it in your controller. A helper should have a general purpose for your application.
In that way, your code will be more flexible and reusable for the future. Messing with 2 Controllers seems bad to me. Remember that with the default Routing system of CI you can be confused.
Try to use following code.
defined('BASEPATH') OR exit('No direct script access allowed');
class B_Controller extends A_Controller {
public function test()
{
$this->display();
}
}
Use construct in controller
defined('BASEPATH') OR exit('No direct script access allowed');
class B_Controller extends A_Controller {
public function __construct() {
parent::__construct();
}
public function test()
{
$this->display();
}
}
I found the solution using including parent controller on child controller like this -
require_once(APPPATH."modules/frontend/controllers/Frontend.php");
then my function like this -
class Home extends Frontend {
function __construct() {
parent::__construct();
}
function index() {
echo $this->test(); //from Frontend controller
}
}
I hope this will help.
add this script in B_Controller :
include_once (dirname(__FILE__) . "/A_Controller.php");
for example B_Controller.php :
defined('BASEPATH') OR exit('No direct script access allowed');
include_once (dirname(__FILE__) . "/A_Controller.php");
class B_Controller extends A_Controller {
}

wiredesignz modular extensions extending controller

I am using WireDesignz modular extensions with great success so far. I now have the need to extend a controller within the module. I have created the new controller and the original, now extended, controller and they work fine outside of the HMVC but when I put them inside the module folder and call the new controller it days is can't find the controller it's extending... even though it's right there in the same directory. If I call the original one it's all fine. I'm not sure where to go with this as I can't find anyone with the same problem online. Any ideas? Here's a bit more:
Original controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Calendar extends MY_Controller {...
New controller, in same directory:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Calendar_new extends Calendar {...
Results in:
Fatal error: Class 'Calendar' not found in /home/d/e/demo/web/public_html/application/modules/calendar/controllers/calendar_new.php on line 2
Thanks.
The base controller class which you are extending is not being included as a resource. Codeigniter will not automatically try to load base classes.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include('Calendar.php');
class Calendar_new extends Calendar {...

Extended core class not found

I have some codeigniter application. It works well on my local server. After I uploaded it to the hosting server it won't work and resulting an error:
Fatal error: Class 'System_Controller' not found in /home/k2113138/public_html/test/application/controllers/login.php on line 3
Here is my System Controller that extending CI_Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//location: application/core/
class System_Controller extends CI_Controller {
public function __construct(){
parent::__construct();
//some code
}
}
my controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//location: application/controllers/
class Login extends System_Controller {
public function __construct()
{
parent::__construct();
}
}
i have set my config as my desired configuration like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['base_url'] = 'http://xx.com/subfolder/';
$config['subclass_prefix'] = 'System_';
?>
I have read many article that discuss this matter but still cannot found the solution. Really confuse about the problem, because everything works well in local server. I have configured the config file as what it's needed like the database and the routes configuration. Is there any other things that i need to re-configure?
EDIT: My Codeigniter version is 2.1.3
Ok, you changed the filename to lowercase, now, try change the classname too:
class system_controller extends CI_Controller
If don't run, play with the classname and the filename, this is a problem of your server, very common in shared servers.
Sometimes, the PHP version and its configuration, play a important rol in this cases.

Fatal error: Class 'CI_Controller' not found

I am trying to run a project but I am getting the below error.
Fatal error: Class 'CI_Controller' not found in E:\wampserver\www\NeoBook\application\controllers\usercp.php on line 2
I checked all the solutions in SO but none worked for me. I am a newbie to CodeIgniter, can anybody help me out in fixing this issue?
Please let me know if you need anymore information about this question.
Also while using
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//Page is responding as `No direct script access allowed`
class Usercp extends CI_Controller {
public function __construct()
{
parent::__construct();
include_once('member/library/Am/Lite.php');
$this->is_logged_in();
$this->load->model('model_facebook');
$this->load->library('pagination');
$this->load->library('Layouts');
$this->load->model('model_user');
}
}
This is the code. Wherever class CI_Controller been used is not working.
You are using PHP tag inside a PHP tag use this (Removed '
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//Page is responding as `No direct script access allowed`
class Usercp extends CI_Controller {
public function __construct()
{
parent::__construct();
include_once('member/library/Am/Lite.php');
$this->is_logged_in();
/// More of your code.
$this->load->library('Layouts');
$this->load->model('model_user');
}
}

Categories