I have this code file in Codeigniter, but the URL method doesn't work also try so really I don't see problem here.
<?= link_tag('asstes/css/bootstrap.min.css') ?>
load through helper $this->load->helper('html');
What am I doing wrong?
On CodeIgniter 3 versions Just check if your base_url is set because some times having that blank can cause links not to work properly.
$config['base_url'] = 'http://localhost/your-project/';
Controller example: Welcome.php
<?php
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('html');
}
public function index() {
$this->load->view('welcome_message');
}
}
Or auto load html helper
$autoload['helper'] = array('url', 'html');
In your head tag on view link_tag guide
<html>
<head>
<?php echo link_tag('assets/css/bootstrap.css');?>
</head>
Folder Example
application
assets
system
index.php
You have an error in your string path
asstes ---> assets
I think it's better to write your own helper for add your resources (js, css...)
Related
I have checked all config files, made sure the helpers are in both system/helpers and application/helpers as well as one or the other. I have tried autoloading helpers, loading the helper in my controller and from the view and no matter what I get an error.
Unable to load the requested file: helpers/_helper.php
It is treating it as if I am trying to load a blank helper such as $this->load->helper(''); but I am using $this->load->helper('url');
This is my controller to load my home page
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('url');
}
public function index(){
$this->load->view('templates/header');
$this->load->view('home');
$this->load->view('templates/footer');
}
}
system/helpers/url_helper.php and application/helpers/url_helper.php both currently exist and have not been edited at all.
If needed I can include my autoload.php but the helper Auto-load Helper Files is currently $autoload['helper'] = array();
If you need to over ride the url helper
rename the file
application/helpers/MY_URL_helper.php
OR
application/helpers/MY_url_helper.php
Copy the whole path inside the url, like this , (wamp)
$this->load->helper('www/projectfoldername/applications/libraries/helpers/helperfile');
i have a problem including a folder/file in codeigniter, this is how the files are structured in my view.
-application
-model
-controller
-view
-auth
-login.php //I want to include header.php, sidebar and footer on this file
-includes
-header.php
-sidebar.php
-footer.php
on the application/view/auth/login.php i have include('../../include/header');
but is not working, i have tried several other means and no job also i cant use an absolute path because the files are in the application folder.
edit:
example in my view folder i have index.php file and i has the following codes
<?php
include('includes/header.php');
include('includes/sidebar.php');
?>
<div id='content'>
<h3>Title</h3>
<p>contents contents</p>
</div>
<?php include('/includes/footer.php'); ?>
Now in the view folder is another folder called auth and i have a file in it called login.php and has the following codes
<?php include(../includes/header.php);?> //this doesn't work
<?php include(../includes/sidebar.php);?> //this doesn't work
<div id='form'>
<form>
</form>
</div>
<?php include(../includes/header);?> //this doesn't work
on my controller i have
<?php
class Index extents CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('index'); //this works and loads all the includes
}
}
another example of a controller not working
<?php
class Login extents CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('auth/login'); //this works but doesn't load the includes
//in the login.php file found inside the auth
//folder
}
}
You can use APPPATH. APPPATH is a CodeIgniter constant. It contains the value of the full path to your application folder.
<?php include(APPPATH.'views/includes/header.php');
//your code here
include(APPPATH.'views/includes/footer.php'); ?>
Take a look a the view loader (http://ellislab.com/codeigniter/user-guide/libraries/loader.html).
Most CI functions are available with inviews, so you can use something like this:
Instead of
include('../../include/header');
use
$this->load->view('includes/header');
This is an old post, but for as many as would stumble upon it.
I'll advice that you move the include folder to your root folder. It shouldn't be in the application folder, but the folder where the application folder is.
Then you replace with
<?php include(base_url(include/header.php)); ?>
That should do the trick.
Currently I display images in the following way:
<img src="<?php echo base_url().USER_UPLOAD_URL.$post['userPhoto'] ?>" />
USER_UPLOAD_URL is defined inside application/config/constants.php.
define('USER_UPLOAD_URL', "uploads/user_uploads/");
Is there any way to include base_url() inside constants.php? In this way I wouldn't need to write each time base_url() inside view. Is there any alternative approach?
tnx
constants.php loading before config.php, so you can't use $config['base_url'] from constants.php.
But you can do something like that:
constants.php:
define('BASE_URL', "http://mysite.com");
define('USER_UPLOAD_URL', BASE_URL."uploads/user_uploads/");
config.php
$config['base_url'] = BASE_URL;
You can't use base_url() in the constants file because the constants file is loaded first, and the base_url() is only loaded when you either autoload the url helper, or load it per controller.
Here's a suggestion though, you could possibly define it in your controller:
public function __construct()
{
$this->load->helper('url');
define('USER_UPLOAD_URL', base_url('uploads/user_uploads/'));
}
Which would then be accessible to the view.
I'd complement the #Dale answer making use of a custom controller. At first place, create the controller at /application/core/GeneralController.php:
<?php
class GeneralController extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
define('USER_UPLOAD_URL', base_url('uploads/user_uploads/'));
}
}
Now, you can extends all controllers from GeneralController instead of CI_Controller, because USER_UPLOAD_URL is now defined
It is an old question still i overcame this issue using APPPATH.
APPPATH works inside constants.php. but it returns path as var/www/html/projectfolder/application inside application folder you can store you data within a new folder or third_party folder.
In constants you can define as eg define('pace-min-js',APPPATH.'third_party/luna/vendor/pacejs/pace.min.js');
I am trying to use a template library with my current codeigniter project. However I am getting the following error message,
Unable to load the requested file: main.php
My file system looks like this,
/application
/themes
/views
/layouts
admin.php
In MY_Controller.php I have the following,
<?php
class Dashboard extends MY_Controller {
public function __construct()
{
parent::__construct();
}
}
?>
In my controller I have the following
<?php
class Dashboard extends MY_Controller {
public function __construct()
{
parent::__construct();
//$this->template->set_layout('admin');
}
public function index()
{
$this->template
->set_layout('admin') // application/views/layouts/two_col.php
->build('welcome_message'); // views/welcome_message
}
}
It depends what template system your using, from the sounds of it you're using Phil Sturgeon's template library.
If so open up config/template.php
and find this line:
$config['layout'] = 'main';
And edit it as applicable.
Which template system are you using? I guess it needs a file named main.php which will load the view. Or the file does not exist and you will have to add it or the path to the file is not correct
Hi everyone I am new to a PHP framework codeIgniter, I am going over the user guide iv ran in to a problem, I am on the part where you load helper file but for some reason my code just in not working I keep getting this error:
Fatal error: Call to undefined function anchor() in /home/fresherd/public_html/CI/system/application/views/blogview.php on line 17
now im not 100% sure that it is loading the helper file this could be causing the but I am not sure how to detect the file has been loaded
any advice will help many thanks, Alan
If you are not sure please check the autoload.php file in the config folder or in your controller put the following function:
<?php
function __construct(){
parent::__construct();
$this->load->helper('url');
}
?>
Just add url in helper inside autoload.php inside config folder.
$autoload['helper'] = array('url');
or you can add this inside your function
$this->load->helper('url');
Just load the helper in your controller or put it in the auto load array.
$this->load->helper('url');
I would also change Gerardo's code to this:
function _construct() {
parent::__construct();
}
loading the helper in the controller solved my problem,, jus try this way
<?php
function __construct(){
parent::__construct();
$this->load->helper('url');
}
?>
or try putting it in autoload array in config.php in the application/config folder., like
$this->load->helper('url');
hope it helps...
Make sure that your controlLer, has the parent statment in the construct
function __construct(){
parent:: ControlLer();
}
You can autoload your helper so you dont have to reload it on every page..
$autoload['helper'] = array('url');
or manually load it on every page..
$this->load->helper('url');