Why is this constructor failing to load in Code Igniter? - codeigniter

<?php
class Blog extends CI_Controller {
function Blog() {
parent::CI_Controller();
}
}
I'm trying to create a constructor in Code Igniter for my class 'Blog' and the above code is giving me a fatal error:
Fatal error: Call to undefined method
CI_Controller::CI_Controller() in
C:\xampp\htdocs\mysites\blog\application\controllers\blog.php
on line 5
How do I fix this?
(I'm going through a online video tutorial on the official code igniter website but I think the tutorial is about 2 years out of date as some of the things are not working when I follow them exactly as shown in the video, this being one of them - the link to the video is here - I encounter this problem towards the end of the tutorial about 8 minutes in)

It should be this...
<?php
class Blog extends CI_Controller {
function __construct() {
parent::__construct();
}
}
The tutorial you are probably going through is based on 1.7.2 which had a core of php4 which did not use the php5 __construct() method of building Class constructors. Codeigniter 2.0.0 has a php5 core and uses it.

Related

How to work with subdirectory controllers in CodeIgniter 4?

I need help with using sub directory controllers in CodeIgniter 4.
I just can't make it work for some reason.
This is the URL for example: www.example.com/admin/dashboard
In the controllers folder, I created a folder named Admin, and a file named Dashboard.php.
I used this code in Dashboard.php:
namespace App\Controllers;
class Dashboard extends BaseController
{
public function index()
{
}
}
I tried changing the class name to AdminDashboard, Admin_Dashboard, pretty much every logical name but every time I get a 404 error, saying:
Controller or its method is not found:
App\Controllers\Admin\Dashboard::index
I know the file itself gets loaded successfully, but I think I don't declare the classname correctly and it keeps throwing me those 404 errors.
The documentation of CI4 isn't providing any information about what the classname should be called unfortunately...
UPDATE #1
I managed to make it work by changing few things:
namespace App\Controllers\Admin;
use CodeIgniter\Controller;
class Dashboard extends Controller
{
public function index()
{
}
}
But now it won't extend the BaseController which has some core functions that I built for my app.
Any ideas to how to make it extend BaseController?
I must admit that I don't have much knowledge about namespacing yet, so that might be the reason for my mistakes.
As I imagined, the problem was that I didn't learn about namespacing.
I needed to point the use line at the BaseController location.
namespace App\Controllers\Admin;
use App\Controllers\BaseController;
class Dashboard extends BaseController
{
public function index()
{
}
}
Now www.example.com/admin/dashboard/ goes directly to that index function, as intended.
php spark make:controller /Subfolder/ControllerName
$routes->add('/(.+?)_(.+?)/(.+?)$', 'subdir\\\\$1_$2::$3');
$routes->add('/(.+?)_(.+?)$', 'subdir\\\\$1_$2::index');
I was able to map with this setting.
The route mapping could be as simple as:
$routes->group('admin', static function ($routes) {
$routes->get('dashboard', 'Admin\Dashboard::index');
});

breadcrumbs in codeigniter (MVC)

I want some help with breadcrumb in codeigniter. It's my first experience with CodeIgniter and I don't have to clear enough how can I declare this function at Controller
public function breadcrumb(){
$this->load->library('breadcrumb');
function Tutorial() {
parent::Controller();
$this->load->library('breadcrumbcomponent');
$this->breadcrumbcomponent->add('Home', base_url());
$this->breadcrumbcomponent->add('Tutorials', base_url().'tutorials');
$this->breadcrumbcomponent->add('Spring Tutorial', base_url().'tutorials/spring-tutorials');
}
}
in View. I create the library as it is showing here:
http://www.technicalkeeda.com/codeigniter-tutorials/how-to-create-bread-crumb-using-php-codeigniter
Help me to understand better and to solve this case!
Best :)
Please edit your question and include proper code tags where needed. I will assume that you are using new version of CodeIgniter, but old tutorial (one that is related to CI version less than 2). Try to change this in code should be working that way.
In controller, change this code:
function Tutorial(){
parent::Controller();
$this->load->library('breadcrumbcomponent');
}
with this one:
public function __construct()
{
parent::__construct();
$this->load->library('breadcrumbcomponent');
}
Library class could stay the same, I'd say.

How can I add an external code or script to Codeigniter as class library?

I have an external script or code, for example, including functions, classes, and database inside it. Is there any way to add it to Codeigniter as class library? If yes, please explain more details as possible. My script has some important files included a number of good functions and classes, even database connection and mysql queries inside, but I am confused about how to add it to Codeigniter without rewriting code lines in Codeigniter. Suggest me some good methods to resolve my problem effectively as possible because I do not want to spend time on rewriting and I am a new beginner of Codeigniter. Thank you very much for your help!
The user guide is pretty well explained
http://www.codeigniter.com/user_guide/general/libraries.html
http://www.codeigniter.com/user_guide/general/creating_libraries.html
go to application/library put your class into
Library file name lib.php
class lib {
public function __construct()
{
}
public function test(){
return "welcome";
}
}
go to application->controllers
class Hall_list extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->library("lib");
echo $this->lib->test();
}
}
In this way you can load library to your controller

configure flexigrid helper, and customize variable $colModel

I try to start Flexigrid into Codeigniter.
I download demo files from here http://gembelzillonmendonk.wordpress.com/2010/06/28/flexigrid-and-codeigniter-with-advanced-searching-with-example/
but i wrong to set up something:
Instruction from site:
extract .rar to CI folder**
open your controller (ie: CI_Folder\system\application\controllers\flexigrid.php)**
configure flexigrid helper, and customize variable $colModel, example:**
code example
What I did:
I installed correctly CodeIgniter (copy and paste inside htdocs - xampp)
I created a database called 'country' and import sql file inside demo.
I copied correctly files an folders from demo files to my CI folder (called grocery-crud-demo).
But when I type on firefox http://localhost/grocery-crud-demo/index.php/flexigrid/index I have this error
Fatal error: Class 'Controller' not found in C:\xampp\htdocs\grocery-crud-demo\application\controllers\flexigrid.php on line 2
A PHP Error was encountered
Severity: Error
Message: Class 'Controller' not found
Filename: controllers/flexigrid.php
Line Number: 2
Backtrace:
I read instruction also from here: http://roadmyapps.toile-libre.org/index.php/flexigrid/examplebut I don't understand which code exactly I must have into flexigrid.php
I don't understand how configure flexigrid helper, and customize variable $colModel.
I need exact code into files. Have you idea?
I find CodeIgniter 1.7.3 -- I repeat steps -- new error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Ajax_model::$db
Filename: models/ajax_model.php
Line Number: 34
Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\grocery-crud-demo\system\application\models\ajax_model.php on line 34
Code of ajax_model.php is this: http://pastebin.com/P3KawC7S
Probably you are using newer version of CI than one on tutorial.
As I can see tutorial is written for 1.7.2 version.
Try to change
class Someclass extends Controller
{
public function someclass()
{
}
public function index()
{
//etc, etc
}
}
with
class Someclass extends Controller
{
public function __construct()
{
parent::__construct()
{
}
}
public function index()
{
//etc, etc
}
}
Also, for models too. Like
public function Ajax_model()
{
parent::Model();
$this->CI =& get_instance();
}
should be
public function __construct()
{
parent::__construct();
$this->CI =& get_instance();
}
Try that way.
Spot those differences. Those should change in every controller and model respectivelly. Follow this link for upgrading your app.
If fail again, see the CodeIgniter documentation about upgrading versions.

CodeIgniter Undefined Variable while trying to create subview

Hi I am new to CodeIgniter and am taking a tutorial. I ran into an error
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: subview
Filename: admin/_layout_modal.php
Line Number: 7
An Error Was Encountered
Unable to load the requested file: .php
I followed the tutorial to the tee and I cant seem to find out where I am going wrong.
here is the controller
<?php
class User extends Admin_Controller {
public function __construct() {
parent::__construct();
}
public function login() {
$this->data['subview'] = 'admin/user/login';
$this->load->view('admin/_layout_modal', $this->data);
}
}
and the view
<?php $this->load->view($subview); ?>
Please help...
If you gave script from your .php file not tutorial then everything is ok.
You are probably typing the wrong url in when you try to access the page. Make sure you are loading "admin/user/login" instead of "admin/dashboard/modal".
If you are following tutplus tutorial- building cms with codeigniter, First watch two episodes, Managing User Part 1 and Managing User Part 2 and then Start building, Your question will be answered on Part 2.... the dashboard has to declare the subview variable and
CREATE THAT admin/dashboard/index.php file in views
class Dashboard extends Admin_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
$this->data['subview'] = 'admin/dashboard/index';
$this->load->view('admin/_layout_main',$this->data);
}
public function Modal(){
$this->load->view('admin/_layout_modal',$this->data);
}
}
don't need to pass data to $this. Just pass the data to
$data['variable_name']=data;
pass this $data variable to view through load class.

Resources