Reputation: 152
Why in relative links I should always use syntax base_url+link in CodeIgniter? For example:
<a href="<?php echo base_url()?>link22.php">
instead of
<a href="/link22.php'>
when I've got declared <base href="<?php echo base_url()?>">
in HTML head?
What are the benefits?
Upvotes: 3
Views: 3414
Reputation:
Just found this out use this if anyone finds this helpful if you're using a custom port on your localhost mention in your
application/config/config.php
$config['base_url'] = 'http://localhost:portNo';
Upvotes: 0
Reputation: 38642
Good question.
Short words (used in below)
CI - Codeigniter
base_url()
??base_url()
is a the function which determine your project destination which act as $_SERVER['REQUEST_URI']
in php. This will know where your actual project contains.
base_url()
??Path - application/config/
File name - config.php
base_url()
function?No. You can't. In CI they having some libraries and helper which help us to use CI more friendly. So to base_url()
there is an helper file call url
.
How to load it??
Path - application/config/
File name - autoload.php
there is function like this $autoload['helper'] = array();
. So add the helper to that
$autoload['helper'] = array('url');
base_url()
??Yes. There are two methods you can define base_url()
Method 01
$config['base_url'] = '';
Method 02
$config['base_url'] = 'http://localhost/foldername/';
Difference between Method 01(M1) and Method 02(M2)
M1 - When we keep empty base_url
it will automatically determine your project scope. So when ever you can't find path or having some doubt this is recommend.
M2 - There is no difference between M1, In here you know path. So can define it.
Which method is good/recommended?
As my suggestion is Method 01 is the best.
echo base_url()
instead of just base_url()
base_url()
is an variable which pre+defined(Codeigniter+User) in CI framework, which we can use entire project. So to show content of variable in php
, we use echo
. In here also same theory comes to this.
<a href="/link22.php'>
will not workCodeigniter Framework build with MVC Structure. (MVC stand for Model View Controller.)
So when open your project it will call to Controller, then if there any incoming values from Database. It will get that from the Model, Then it will load relevant view which mentioned in Controller.
If this link22.php
is some related file to your project you have to place that inside View folder.
Yes. you can. For that you have to use APPPATH
.
Example - Assume link22.php
is inside view/links/
So you have to use
APPPATH.'view/links/link22.php'
Some Usefull links
Upvotes: 2
Reputation: 3158
there are times when you are developing you will have a folder structure for different versions like: localhost/dev/ or localhost/beta/
and your live website might be a different naming like epicapps.com/preview/
by using base_url() you can map to localhost/beta/ on your local copy, and epicapps.com/preview on your public server.
hint: you can set base_url on your main index.php page. like
$assign_to_config['base_url'] = 'http://localhost/beta/';
and make sure that in application/config.php the base url is blank. $config['base_url'] = '';
then you can push any changes from your local application folder to the server application folder, and the config file in applications will not mess you up, because base_url is being defined in the main index.php file.
Upvotes: 3