"Unable to load the requested file" - php

Using CodeIgniter 2.0.3 on XAMPP 1.7.7 I have code where I get the error as: Unable to load the requested file: home.php
The home.php code as follows stored in ./ci2/application/controllers:
class Home extends CI_Controller {
function Home()
{
parent::__construct();
}
function index()
{
$this->load->view('home');
}

While using it just notice:
controller's name is home.php
File named home.php must be present in ci/views
Incase file extention in view folder is not php eg like its html.
then load it using : $this->load->view('home.html');
the function u need to call from outside must be public So make it:
public function index() {... }
as u are calling the constructor for the class Home. Call it like:
class Home extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('home');
}
Now it will work !

Your call to $this->load->view('home'); will be looking for home.php in /ci2/application/views/. That's the file it can't find.
I'm assuming you're calling http://myapp/index.php/home/ - that means it'll automatically call the index() method.

class Home extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('home');
}
}
Please check the name of the file you have created in application/views/, because otherwise the code is correct.

Related

Codeigniter/PHP include re-useable functions in controller

I have this function that is constantly being reused in my controllers. I have decided to move it into a file that can be referenced all the time. This is my file structure
controllers
|Generic
|Users
|get_all_languages.php
|Users
|Lang
|Lang.php
I want to reference get_all_languages which contains
<?php
function get_all_languages(){
$this->curl->create(GetAllLanguages);
$this->curl->http_login(REST_KEY_ID,REST_KEY_PASSWORD);
return json_decode($this->curl->execute(),true);
}
So far, I have tried including it in the top of my file like:
<?php
include __DIR__.'/../../Generic/Users/get_all_languages.php';
class Lang extends CI_Controller{
However, when I try to use that function like $this->get_all_languages();, an error occurs saying Call to undefined method Lang::get_all_languages()
I have also tried to include it after the __contruct but it doesn't allow me to compile.
I hope someone can let me know how I can reference that function.
Thank you.
You can use library or helper of codeigniter.
And you can load them automatically in application/config/autoload.php.(Reference it)
If you want the specific controller, you can use it in construct of controller using $this->load->library() or $this->load->helper().
For example:
class A extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('libraryname');
$this->load->helper('helpername');
}
public function index() {...}
...
}
...
Updated
application/helpers/global_lang_helper.php
<?php
function get_all_languages(){
$CI = &get_instance();
$CI->load->library('curl');
$CI->curl->create(GetAllLanguages);
$CI->curl->http_login(REST_KEY_ID,REST_KEY_PASSWORD);
return json_decode($CI->curl->execute(),true);
}
At your controller...
public function __construct()
{
parent::__construct();
$this->load->helper('global_lang');
}

Codeigniter Error Unable to locate the specified class: Loader.php

i am trying to make a core class in my codeigniter but its giving error that Unable to locate the specified class: Loader.php.
My Core class is MY_base.php and code is.
class MY_base extends CI_Controller{
public function load_header(){
$this->load->model('mod_practice');
$headData=$this->model->get_header();
$this->load->view('header',$headData);
}
}
My model Mod_practice.php code is
class Mod_practice extends CI_Model{
public function get_header(){
$query = $this->db->get('header');
$result = $query->result_array();
return $result;
}
}
My home.php ( main controller) code is
class Home extends MY_loader{
function index(){
parent::MY_base();
}
}
but when i try to run Home controller its giving me the following error
Unable to locate the specified class: Loader.php.
Where can be the error ? Thanks in advance.
You are doing something wrong. You need to construct CI_controller first.
class MY_base extends CI_Controller{
public function __construct()
{
parent::__construct();
// Your own constructor code
}
public function load_header(){
$this->load->model('mod_practice');
$headData=$this->model->get_header();
$this->load->view('header',$headData);
}
}
And now you can do this:
class Home extends MY_base{
public function __construct()
{
parent::__construct();
// Here you have access to load_header() function
}
}
Ou - and also your create MY_base and then refer to MY_loader.
may bee you can use HMVC Codeigniter https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc

CodeIgniter controller won't load a helper

I created a helper in CodeIgniter 3 which I called asset_helper.php
and this is the controller I use to call this helper:
<?php
class Index extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('asset');
}
function index()
{
$this->load->view('header');
$this->load->view('nav');
$this->load->view('title');
$this->load->view('stat1');
$this->load->view('footer');
}
}
but when I access to the controller I get this message:
Unable to load the requested file: helpers/asset_helper.php
How can I solve this problem?
Load like this
$this->load->helper('asset_helper');
If you use like this $this->load->helper('asset'); it will never works.B Bcz there is no file call asset

Codeigniter not finding file url helper

So I have this setup:
ARTISTAS
But when I click on that link, it takes me to http://localhost/Universal%20Music/Universal/artistas/index which says 404, like if there's nothing there but I've already created the controller with that name loading some views in it. Am I missing something here?
EDIT
My config.php has:
$config['base_url'] = 'http://localhost/Universal/Universal';
I stop using spaces in the names of the folders and now the path is:
C:\Users\Horacio\Documents\Projects\Universal\Universal
The controller file has the same name of the class I need and the code inside is:
<?php
class Artistas extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index()
{
$this->load->view('header');
$this->load->view('main-index');
}
}

controller name in the url in codeigniter to be more than one

I have 3 controllers are home, food, and drink. home controller I want to make the default controller. and I wrote in routes.php file like this:
$ route ['default_controller'] = "home";
$ route ['404_override '] ='';
then i made the code in each controller as below:
controller home.php
class Home extends CI_Controller{
public function __construct() {
parent::__construct();
}
public function index(){
}
public function info(){
}
}
controller food.php
class Food extends CI_Controller{
public function __construct() {
parent::__construct();
}
public function index(){
}
public function foodMenu(){
}
public function foodJenis(){
}
}
controller drink.php
class Drink extends CI_Controller{
public function __construct() {
parent::__construct();
}
public function index(){
}
public function drinkMenu(){
}
public function drinkJenis(){
}
}
I want to make this url like www.mydomain.com/home, www.mydomain.com/food, www.mydomain.com/food/foodmenu, www.mydomain.com/food/foodjenis, www.mydomain.com/drink, www.mydomain.com/drink/drinkmenu, www.mydomain.com/drink/drinkjenis.
but when I was on the url www.mydomain.com/food/foodmenu and I headed to www.mydomain.com/food/foodjenis, but the url that appears is www.mydomain.com/food/food/foodjenis. "food" controller name in the url it into two. how to handle it. please help me. thank you :(
One way to solve is through the use of base tag.
Somewhere in the document head (before including any css or js) put
<base href='http://mysite.com'>
After that you can freely use relative links in your CI applications.
How do u make your links?
I think there is problem u try to make relative linking and html is looking at food as a folder so when u make link as
it will make link as u wrote www.mydomain.com/food/food/foodjenis
But if u use CI url
It shall work like u intended.
did you set base url in config file in CI ?If yes. try with relative paths. like food/foodjenis

Categories