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');
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 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...)
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');
I have a list of constants (I'm using them as an enum), some are define statements, and some are just global variables.
Where am I suppose to put them in the MVC framework so I can use them for both my model and my controller that needs to reference it?
I'd rather not stick it into config/constants.php since they shouldn't be called except for by this model and the controllers that use it.
Edit 1: Clarification
To be more specific, I have my message_model model and it has a bunch of constants that I need that are stored in message_model_constants.php. Where should I put message_model_constants.php and is there a way to have it automatically included by the controller that loads message_model when message_model is not (and I don't want it to be) auto-loaded.
Edit 2:
I really don't want to have the constants auto-loaded except for when I use the model
Go to application/config/constants.php and define your constant their and you can use your constants on Model-View-Controller of CI not include "Helper" and "Library"
But in your case I prefer you to create a php file that has your constants and rename it to something like my_constants_helper.php.
In your model or controller __construct just
$this->load->helper('my_constants');
And hooray you can access them =)
You can choose to load a particular config file when you load a particular model in the controller. For instance in your file:
application/controllers/messages.php
You would use a line like this:
$this->config->load('messages');
If you include it at the top of your controller like this
function __construct() {
$this->config->load('messages');
$this->load->model('message_model');
}
Then all of those constants will be available to all the functions and methods in the given controller. You then call each config constant like:
$this->config->item('item name')
And you can name protected $variables; in the construct as well for shorter syntax.
If you are using these config constants and the message model in multiple different controllers you may want make a "Library" file that then loads both the config and the model and declares all variables there.
extending Brennan Novak answer, you can simplify your code by loading your config file in the model constructor. That way, you only have to load the model in your controllers and everything else is done automatically.
Model
class Message_model extends Model {
function __construct()
{
parent::Model();
$this->load->config('message_model_constants');
}
...
}
Controller
class Some_controller extends Controller {
function __construct()
{
parent::Controller();
$this->load->model('message_model');
}
...
}
As already stated, your config files should be application/config/message_model_constants.php
For global configs, add to the config/config.php or create a config/application.php and then load the config, then the item.
$this->config->load('application'); // or autoload this - $autoload['config'] = array('application');
$this->config->item('item name');
Have you considered just adding your constants to your Message_Model Class? You'll then reference them by self::ConstantName inside the Class and Message_Model::ConstantName outside the class. This would also prevent name space collision.
I am using $data in all my views $this->load->view('my_view', $data);
I have also autoload a Controller following this guide Extending Core Controller
But I want to make $data global because in views there is a sidebar which is constant for whole project and displays info fetched through db in autoloaded controller
Currently I have to manually write $data['todo'] for each and fetch info from autoloaded model.
Thank You.
1: Create MY_Controller in application/libraries with following:
class MY_Controller extends Controller {
var $data;
//constructor function
}
2: Replace Controller to MY_Controller in all your controller files and load views with $this->data
class Contact extends Controller { //to.. }
class Contact extends MY_Controller {
$this->load->view('contact_view', $this->data);
}
this way you can perform default functions that are applicable for whole site in MY_Controller like loading settings.
I ran into a similar problem earlier today. I found that an easier way, rather than globals, was to use constants. You can define a constants file that will load from your index.php file:
// Include additional constants
$defines_file = 'includes/defines.php';
if (file_exists($defines_file))
{
require_once($defines_file);
}
Then you can add your constants to the defines.php file:
define(MY_CONSTANT,'my constant info');
This way they will be available in any file throughout the system either directly: echo MY_CONSTANT; or you can assign them to variables.
I decided this way would be easier for me as I would only have 1 location to go to when/if I needed to change the constants.
More: http://codeigniter.com/forums/viewthread/56981/#280205
I used a helper function to call a global function!
eg.
function get_user($userid){
$CI =& get_instance();
$query = $CI->db->get_where('users', array('id' => $userid), 1, 0);
foreach ($query->result() as $row){
// Return a object with userdata!
return $row;
}
}
Now I have access to my userdata everywhere..
Rather than making the view data global, I'd recommend using HMVC to build a module to produce this sidebar view. HMVC is a nice clean way of coding partial views.