CodeIgniter - Which function to use for URL's - codeigniter

Im building my first application using CodeIgniter, i need a bit of advice.
There are 2 functions that do the same thing and i was wondering which is the best to use.
Ok, so usually when im building a site, i link to the homepage by a simple / but if building on a directory, it will take back to the root public_html directory, with codeigniter i have found both
site_url()
and
base_url()
but, they both seem to do the same thing .. Just wondering if theres any difference, which one is better to use, etc etc.
Cheers,

If you are using index.php
site_url()
will include the index.php , and
base_url()
will not include it.
If you are creating a url to pass, use
site_url('images/img.png')
otherwise...
base_url().'images/img.png'

Related

When do we need to use base_url() function in codeIgniter?

I am working on a small project and all of my urls are written without base_url() function. So I was thinking what does those base_url() functions help with my urls. I know it will prefix the url based on configs file preferences. But simply I wanna ask
What is the advantage of using them ? and whats the disadvantage of not using them ?
The main advantage is that; when you want to migrate your project to online or in a different server; you won't need to edit your hard-coded links. The base_url() will make your inside links to get their base url dynamically.
echo base_url("blog/post/123");
will return
http://example.com/blog/post/123
in example.com and will return
http://jms.com/blog/post/123
in jms.com
base_url is needed when you create login/signup with email verification or forgot password functionality (Verifying by clicking the links like this http://yourdomain.com/auth/userid/code).
This is only one example, may have lot of uses.
the difference of base_url() and site_url() is that
both returns the url of your site but site_url() returns your site url, with attached index.php or on what you have configured $config['index_page'] = ''
in application/config/config.php
example www.yoursite.com/chicken/turkey
when using base_url(): Returns your site base URL, as specified in your config file.
you will get wwww.yoursite.com
when using site_url():
you will get www.yoursite.com/index.php
site_url() is usefull if you haven't change your .htaccess and you still access your controllers with index.php
base_url() is usefull when linking your css,js,or media stuffs. Granting that you still use index.php when accessing your controller/method
Its needed every-where, or at least we should use everywhere. Do you know, absolute url is far better then relative url? So, when you are using an absolute urls for all links on your site, instead of hard coded root domain name, you can use the "base_url()" function. Moreover, CI reactor is smart enough to detect the original base url, so you even don't need to enter it in config file.
I usually using the base_url() for the correct paths to the static files (css, js, img) files and the site_url() for the links to pages. site_url() will return with url_suffix witch could be set in application/config/config.php file
Advantage:-if you not use base_url() and you want to switch directory/server of your site , you should change the path all over the project.
if you use base_url(), just change it at onece in config.php ,effect on all site.

How routing is done in codeigniter?

I could not find the .htaccess file in CodeIgniter framework and i'm wondering how do they do route without it? Or it is somewhere hidden?
There is no .htaccess by default provided with codeigniter. They serve it through the index.php. .htaccess is only used to direct the urls to the index.php. This way codeigniter is working in environments where rewriting isn't allowed. Another way of getting controller and function is by query strings.
Anyway the actual routing is done in system/core/Router.php so you can read the code. Since Codeigniter is open source application that can't be hidden.

Codeigniter anchor can't find link

I am using CodeIgniter to design a website, and I am having trouble getting anchor to work correctly.
Here is how I am using it. It is used in a file in views folder of application in codeigniter.
<?php echo anchor('/profile', 'PROFILE'); ?>
My intention is to have anchor load the profile controller, which is in the controllers of application in codeigniter. However, when I clink on the link, it says that the the file is not found.
I autoload the url helper functions alreader, and other functions from that file are working, like site_url() .
I don't know what I could have missed? Do you ave any suggestions? This is my second project with codeigniter, so I am still learning.
EDIT: Yes, I am following the naming convention in codeigniter, and the file has an index function. I tried without the forward slash and it still gives the same result.
The HTML link that this produces is localhost/profile. This is what I Should get right? Since for codeigniter, it's url/controller/function. I did a mod rewrite to remove index.php from the url, but that shouldn't be a problem, should it? I'll try and check the base url again.
Are you sure you need the leading forward slash?
Have you tried:
<?php echo anchor('profile', 'PROFILE'); ?>
You would only be using the slash if you are trying to get into the "profile" sub folder inside your controllers folder.
What helps me when urls get confusing is to type into the browser the full url to the path I want to get to (to make sure I'm not getting a 404, etc). From there you can start from the end and go back to see what you are missing.
But you generally want to start with a controller name and add the function you are calling like:
anchor(controllerName/functionName)
See CodeIgniter URL Helper for examples.
EDIT: also - you may need to check what your config base_url is set to. if there's fancy .htaccess stuff happening, your base_url needs to be set properly.
There is a very simple solution go to config/constants and define SITE_URL there and set its value to
http://localhost/myprojectname/index.php.
Then use it here like this.
<?php echo anchor(SITE_URL.'/profile', 'PROFILE'); ?>

code igniter framework guidance for beginners

I am just a beginner in code igniter. I have just downloaded the code igniter framework. But i don't know that where should i keep my html, php files and stylesheets, images etc. Is there any procedure to do the things? Please guide me.
CodeIgniter has an awesome user guide that will come with the install you can read through (or read it online at - http://codeigniter.com/user_guide/ ), or you can watch some of the videos on their site - http://codeigniter.com/tutorials/
In comparison to most other frameworks you're going to find they have maybe the smallest learning curve and great documentation. I would also recommend learning basic PHP and getting familiar with your web environment maybe before beginning.
I used these tutorials to help get me started with the framework haven't look back since! http://net.tutsplus.com/sessions/codeigniter-from-scratch/
hi I am a weekend coder and picked up CI about a year ago.
It helped me a lot.
The best tutorial I found was on the IBM's developer site. It runs through putting together a simple application. Admittedly, there's nothing in it about directory and file placement but it helps cement ideas about how models, views and controllers (MVC), and why MVC is so helpful. Because the basic idea is quite simple, it's worth running through a simple CI tutorial again and again till you 'feel' or intuit the basic helpfulness of the setup.
Things like JS, CSS files can be kept in their own folders at the first level of your website folder e.g /js or /css or /images. You ask about PHP files as well. PHP files which you, the coder, write, are either 'views', 'models' or 'controllers'. These go in the folders with those names in the /application folder e.g /application/views/yourview.php or /application/controllers/yourcontroller.php. The CI install comes with a default view file and a default controller, which you are probably already aware of.
The files inside /application/config are important as well. Read the user-guide about tweaking these files. The most obvious tweaks are to database.php to connect to your db, autoload to give automatic use of CI helpers/libraries which you can choose, and to config.php to give CI the name of your website e.g the name you give to '/'.
The file 'index.php' comes with the CI installation (/index.php). You don't need to fiddle with it at all really except to determine the level of error reporting you want ('environment') and that's not a priority at all. But it's important to remember this about index.php - that CI uses it as the essential reference for defining paths to useful folders like CSS or images. So even if your view file is in /application/views, if it refers to an image like a logo.gif in /images for example, the path to it is just /images/logo.gif. It is not anything more complicated like ../../images/logo.gif.
I hope that helps.
Tom
Offline version of CodeIgniter user guide is available with CodeIgniter which already downloaded by you.
just extract your CodeIgniter zip file in your localhost server root directory,
Then http://localhost/www/CodeIgniter_2.1.2/user_guide/ open this url with browser ,here you can access offline version of CodeIgniter user guide.
Here I am using wamp server so I used this url, If you are using xampp server then please use http://localhost/CodeIgniter_2.1.2/user_guide/
Okay this is what I would usually do for code igniter
here is my directory structure.
CI App Path (e.g "c:\xampp\htdocs\ci_app_name" )
-application
-system
-assets
--css (new folder , where css files will be included)
--js (javascript and jquery libraries location)
Basic HTML and PHP files should be location
CI_app_pah
-application
--views (this is where to put HTML and PHP files)
For other things such as Controller , Models and Views , you can't put any where but put in their related area.
That will be
CI app
-application
--controllers
--models
--views

Loading all codeigniter controller at the beginning

I've a little problem.
In a controller i will load a function wich is in otherController.
I just use
$pCtrl = new otherController;
for loading the class in that otherController.
The problem is that at that moment the otherController has not already been loaded. So the class is not available.
Can i find a way to tell codeigniter to load all controllers (folder controller) before doing other thing ?
Thanks for help.
This is bad practice. You should be using a CodeIgniter Library to do this instead.
Though, if you're already doing bad things, you could just open the /controllers directory in your config/autoload.php and configure codeigniter to autoload all available controllers. Not sure if this will work well (and efficiently) in practice, so try using libraries.
Its better to create a helper function or a library ..
oh btw , you can use HMVC also .

Resources