Fatal error: Call to undefined function site_url() - php

I am using the CodeIgniter framework for PHP. I have created a view named "login.php". Once I
created the view, I then loaded the view inside of a function named "index" which is located
inside a class named "CCI" that extends the Controller but I keep receiving this error: Fatal
error: Call to undefined function site_url() in C:\wamp\www\FinalP_CCI_Clone\system
\application\views\login.php on line 12. I don't understand the issue I an having because the
welcome page loads fine and my second function inside of the "CCI" class loads fine as well.
Here is some of the code:
Controller Files:
function CCI()
{
parent::Controller();
}
function index()
{
$this->load->view('login');
}
function test()
{
echo "Testing Data";
}
}
/* End of file login.php /
/ Location: ./system/application/controllers/cci.php */
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
$this->load->view('welcome_message');
}
function test()
{
echo "Testing Data";
}
}
/* End of file welcome.php /
/ Location: ./system/application/controllers/welcome.php */

You have to load the helper. The function site_url() is provided by the url helper, as described here.
$this->load->helper('url');

You can try this:
First Load the URL Helper with:
$this->load->helper('url'); or set the following value in application/config/autoload.php
$autoload['helper'] = array('url');
Then you can show the site url with:
base_url() (result: http://example.com/) or
site_url() (result: http://example.com/index.php/)
Note: Results depends on values stored in application/config/config.php

Related

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');
}
}

"Unable to load the requested file"

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.

Fatal error: Call to undefined function validation_errors() in

Here is the code:
class CCI extends Controller {
function CCI()
{
parent::Controller();
}
function index()
{
$this->load->helper('url');
$this->load->view('Users/login');
}
function test()
{
echo "Testing Data";
}
}
The page was loading fine until I moved the location of the "login" page inside of the "Users" folder
Well then probably change
$this->load->view('Users/login');
to
$this->load->view('login');
Since you moved the file inside the Users folder you need to adjust paths for that.
You're properly displaying any validation errors within your view using the 'validation_errors' function that's located in the 'form_helper'. Load the 'form_helper' as well and you should be fine.
Add following two lines of code to the controller:
$this->load->helper('form');
$this->load->library('form_validation');

Error when trying to load view in my_controller

Consider my code:
<?php
class MY_Controller extends Controller {
public function __construct()
{
parent::Controller();
}
function _displayPage($page, $data = array()) {
$this->load->view('structure/header', $data);
$this->load->view($page, $data);
$this->load->view('structure/footer', $data);
}
}
?>
page.php
<?php
class Page extends MY_Controller {
function __construct() {
parent::__construct();
}
function index() {
$data['content'] = array('title'=>'hello world');
$this->_displayPage('home', $data);
}
}
?>
Upon loading my page on my browser, I get this error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Page::$view
Filename: libraries/MY_Controller.php
Line Number: 11
Does anyone know what I'm doing wrong?
Thanks
In your library My_Controller you should be using the parent keyword instead of $this.
your code should look like so:
class MY_Controller extends Controller
{
public function __construct()
{
parent::Controller();
}
function _displayPage($page, $data = array())
{
parent::load->view('structure/header', $data);
parent::load->view($page, $data);
parent::load->view('structure/footer', $data);
}
}
If I understand what you're trying to accomplish correctly, you're wanting to setup a template that includes your header view and footer view, but without calling header and footer views for each controller you use throughout your application. Here's what I've done to accomplish this.
First, create a new folder under your views, for this example we'll call it 'includes'. Inside the newly created includes folder, create three files, header.php, footer.php and template.php. Setup your header and footer appropriately and then edit your template.php to look as follows:
<?php echo $this->load->view('includes/univ_header'); ?>
<?php echo $this->load->view($main_content); ?>
<?php echo $this->load->view('includes/univ_footer'); ?>
Now, from your controller you can define what view you would like to set as your 'main_content'. For example, if you have home.php in your views folder and you want to wrap it with your header and footer you would do so in your controller as follows:
function index() {
$data['content'] = array('title'=>'hello world');
$data['main_content'] = 'home';
$this->load->view('includes/template', $data);
}
Hope this helps!

Categories