Codeignite 404 error - codeigniter

I ham writing the follow Codeigniter code, but I keep getting a 404 error. Any thoughts?
funding_controller.php
class Funding extends CI_Controller {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function index()
{
$this->load->model('funding_model');
$data['funds'] = $this->funding_model->getAll();
$this->load->view('funding_list', $data);
}
}
funding_model.php
class Funding extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function getAll() {
$query = $this->db->get('funds');
return $query->result();
}
}
funding_list.php
foreach($funds as $row) {
echo $row->opportunity_name;
echo "<br />";
}
The default route points to funding_controller. Thoughts?

controller name and class name should be same and file name in lower case
funding.php
And try following url
http://domain.com/funding/index
Model class name different from controller name like funding_model or anything else and no need to suffix _model in ->load->model()

Use first letter in capital for controller n model file name.
For example:
Founding_controller.php
Founding_model.php
And class name should be filename (as it is).

Related

Type: Error Message: Call to undefined method Main_model

I am not able to figure it out the error can any one help me ..
Below is the code.
main_model.php
<?php
class Main_model extends CI_Model {
// function fetch_data($selecteddate){
// //$query = $this->db->query("SELECT * FROM `userLocationLogcat` WHERE id IN ( SELECT MAX(id) FROM userLocationLogcat GROUP BY email )");
// return 0;
// }
public function getUsers(){
return 0;
}
}
?>
welcome.php(controller)
public function one(){
$this->load->model('main_model');
$data = $this->main_model->getUsers();
echo $data;
}
please help.
Please change your model file name. first letter always capital ,
for example main_model.php to Main_model.php
Check that the filenames are capitalized on the first (and only the first) character, such as Main_model.php as it is a convention CI expects and enforces
Make sure your model filename is Main_model.php
If you want to call Main_model from controller you can try use __construct()
public function __construct() {
parent::__construct();
$this->load->model('main_model');
}
public function one() {
$data = $this->main_model->getUsers();
echo $data;
}

Codeigniter 3 url route with independent controllers

I have created a web application with Codeigniter, and I have a problem at the url, controller and structure level.
I have the following web structure.
http://projectroot/admin
Then I have several sections like:
http://projectroot/admin/users
http://projectroot/admin/profile
http://projectroot/admin/section_tracking
http://projectroot/admin/section_products
etc...
I'm working with sessions and other libraries
Currently I have everything in a single controller called Admin, but I would like to create independent controllers that would be calling each part of the url.
In Admin I have:
class Admin extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('My_PHPMailer');
$this->load->library('session');
}
public function index(){
if($this->session->userdata('login')){
if($this->session->userdata('urlnow')){
$url = $this->session->userdata('urlnow');
redirect($url);
}else{
redirect('admin/index');
}
}else{
$data = array();
$data['usererror'] = $this->session->flashdata('usererror');
$data['passerror'] = $this->session->flashdata('passerror');
$data['message'] = $this->session->flashdata('message');
$this->load->view('admin/index', $data);
}
}
...
public function users() {
code....
}
public function profile() {
code....
}
public function section_tracking() {
code....
}
public function section_products() {
code....
}
My idea is that the controller folder contains something like this:
admin.php
users.php
profile.php
section_products.php
section_visits.php
Creating independent Admin extends classes (user, profile, section_tracking and section_products) as independent controllers outside of admin, with a structure similar to this:
users.php
class Users extends Admin {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('My_PHPMailer');
$this->load->library('session');
}
public function index(){
code here...
}
}
profile.php
class Profile extends Admin {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('My_PHPMailer');
$this->load->library('session');
}
public function index(){
code here...
}
}
How can I do it? I don't want to use HMVC, I just want with MVC native.
Thank you
In your application/controllers/admin folder...Make following files
Admin.php
Users.php
Profile.php
Section_products.php
Section_visits.php
Then make call to each files like this...
http://projectroot/admin/users
http://projectroot/admin/profile
http://projectroot/admin/section_tracking
http://projectroot/admin/section_products
etc...
And here you want to extends admin controller.so make Admin_controller.php by extending CI_Controller in application/core. Your `Admin' controller must be like this....
class Admin_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
}
Then always extends Admin_controller.CI_Controller automatically extended.

How do I make a function available to several CodeIgniter methods?

CodeIgniter 2.1.2
I have a class that contains two methods, for the purpose of this questions showone and view. The latter returns all items of a small database and can also perform a search. The other one is for permalinks like domain.com/showone/firstname-lastname
<?php
class Pages extends CI_Controller {
public function view($page)
{
//this includes a mysql search
}
public function showone($slug)
{
//abbreviated version:
$query = "SELECT * FROM mytable WHERE slug = '" . $slug . "'";
$result = $this->db->query($query);
if ($result->num_rows() == 0)
{
//here is where I'd like to use the same search that I used in showall
}
else
{
//show the one item
}
}
} //class
?>
So if a user decides to directly enter a URL that doesn't return anything from the database, I would like to direct him to search results instead of showing a 404.
So how do I set up a function searchdatabase($query) to be used by both showone and view?
You can use your controller functions inside your controller:
public function view($page)
{
$this->showone($slug);
}
Define that function in your model, load your model and then call the model->method.
<?php
//Your Model would look something like this.
class Search_Model extends CI_Model {
public function __construct(){
parent::__construct();
}
public function showone($slug)
{
//abbreviated version:
//Its best to use active record for building your queries
$this->db->where->('slug', $slug);
$result = $this->db->get('mytable');
if ($result->num_rows() == 0)
{
//here is where I'd like to use the same search that I used in showall
}
else
{
//show the one item
}
}
} //class
And then in your controller you would do this:
<?php
//Your Controllerwould look something like this.
class Index extends CI_Controller {
public function __construct(){
parent::__construct();
//model will be loaded for each method.
//if you're going to use this model across several controllers
//its best to autoload it, set that in autoload.php under /app/config
$this->load->model('search_model');
}
public function index(){
$searchResults = $this->search_model->showone('slugone');
}
Update
I just realized you are wanting to show all results if no results were returned. In which case you would perform that logic in your model as well..
In your conditional statement you would do the following:
if ($result->num_rows() == 0)
{
return $this->showall();
}
else
{
return $this->view($slug);
}
}
your showall() and view() methods would return $result->result();

HMVC Module Error - Undefined property: model

So I'm getting the error: Undefined property: badge_progress::$bp_model.
I don't understand what's going on. Here is my code:
Controller:
<?php
// Badge Progress Module
class badge_progress extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('bp_model');
$data['dotpoints'] = $this->bp_model->dotpoints('1');
$this->load->view('bp_view', $data);
}
}
?>
Model:
<?php
class bp_model extends CI_Model {
function dotpoints($badge_id) {
$query = $this->db->query("SELECT * FROM course_topic_dotpoints WHERE badge_id = ".$badge_id);
if ($query->num_rows() > 0) {
return $query->result();
}
}
}
?>
Ah fixed it! Didn't realise that the main controllers (controllers outside of the module directory) also needed to be extending "MX_Controller" instead of "CI_Controller".
Class names must begin with an uppercase letter.
class Badge_progress extends...
class Bp_model extends...
http://codeigniter.com/user_guide/general/controllers.html
http://codeigniter.com/user_guide/general/models.html
update:
You shouldn't have the logic you need as a function in your constructor. Create a separate function to process the dotpoints stuff.
<?php
// Badge Progress Module
class Badge_progress extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('bp_model');
}
function dotpoints()
{
$data['dotpoints'] = $this->bp_model->dotpoints('1');
$this->load->view('bp_view', $data);
}
}
Also, you are missing the constructor in your model. Check out those links I posted earlier...

How to Inherit A Model from Another Model in CodeIgniter

i'm using codeigniter for my project and i have this class model which i call Genesis which looks like this:
class Genesis_model extends CI_Model {
function __construct() {
parent::__construct();
}
function get() {
return 'human soul';
}
}
and i have another model, stored in the same directory, which extends Genesis_model
class Human_model extends Genesis_model {
function __construct() {
parent::__construct();
}
function get_human() {
return $this->get();
}
}
Human_model is used by Human controller
class Human extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('human_model');
}
function get_human() {
$data['human'] = $this->human_model->get_human();
$this->load->view('human/human_interface', $data);
}
}
if i execute the code, it will produce an error which point to return $this->get(). it reads "Fatal error: Class 'Genesis_model' not found in ...\application\models\human_model.php on line 2".
i use this method because nearly all my models shared almost identical structure. I gather the similar functionality in Genesis while the other models serve only as data suppliers unique to the tables they represent. it works well in my asp.net (vb.net) but i don't how to do it in codeigniter.
is there a way for Human_model to inherit Genesis_model. i don't think i'm allowed to use include('genesis_model.php'). i don't know if it works either.
thanks in advance.
core/MY_Model is good if there's only 1 important superclass for your models.
If you want to inherit from more than model superclass, a better option is to change your autoload configuration.
In application/config/autoload.php, add this line:
$autoload['model'] = array('genesis_model');
Put the file genesis_model.php in the core directory
Change your Human_model to this:
include('genesis_model.php');
class Human_model extends Genesis_model {
function __construct() {
parent::__construct();
}
function get_human() {
return parent::get();
}
}
notice the get_human function and the include.
You have to include the Genesis_model on your Human_model.php like this:
include_once( APPPATH . 'folder/file' . EXT );
Or you can autoload it on your config/autoload.php file, what I think is stupid =)
other solution
<?php
$obj = &get_instance();
$obj->load->model('parentModel');
class childModel extends parentModel{
public function __construct(){
parent::__construct();
}
public function get(){
return 'child';
}
}
?>

Resources