Welcome to codeigniter beginners series day 2. Today we'll be installing codeIgniter, go over some very basic codeigniter concepts, we'll try loading some basic pages and make changes to it then learn about routes, passing data and setup a route for our new page aswell.
Lets get started. First go head and install codeIgniter, here's a tutorial I wrote on installing codeIgniter on linux. http://www.webdevtown.com/2016/02/installing-codeigniter-on-linux-ubuntu.html If you're on windows it's still the same steps you'll follow to install. Here's codeIgniter official documentation its installation. http://www.codeigniter.com/user_guide/installation/index.html
With that aside, lets move on to more useful stuff. Open 'localhost' on your browser, make sure apache server is running. You should see a basic welcome page. This is codeigniter's default route. Means anyone who loads localhost will see this page.
Some code!
Lets open our controller folder and see what's inside Welcome.php controller file. I'm assuming you've read MVC if you haven't then you should do so to understand this tutorial. Read it here http://www.webdevtown.com/2016/02/mvc-model-view-controller-seriously.html Alright, we're in Welcome.php file, this a a file that is loaded by default. Whenever someone opens localhost, this file is run. This file contains a class that extends from CI_Controller class, means all CI_Controller's method and properies are available to this class. If we want to create a controller we just give our class a name and extend it form CI_Controller class.
General URL structure is like this. domain/CONTROLLER_CLASS/CONTROLLER_CLASS_METHOD/PARAMETERS. In our case controller class is Welcome, method is index and parameter is welcome by default unless user gives one.
class Welcome extends CI_Controller{
function index($page = 'welcome'){
$this->load->view($page);
}
}
Proceeding, we're $this->load->view('welcome'), when the index method is run, it will run the view method giving it welcome argument, welcome is name of our view page which is inside view folder. If user provided no argument in URL welcomepage would be loaded by default, else whatever page user asked for in argument.
What're routes?
Route is a the route to controller. It means what controller to load depending on what information user provides in URL. As I mentioned about typical URL consists on following division domain/CONTROLLER_CLASS/CONTROLLER_CLASS_METHOD/PARAMETERS. Lets create an test URL. http://localhost/index.php/data/names/jon. Now this url is suppose to return information about user jon. As, jon being parameter, names the method and data the controller class name. Lets say you don't like this long URL you want jon's information when user types http://localhost/user/jon this is where routes will come into play. We'll tell our route file, whenever use types /user/USERNAME in URL, run the data class, username method and give it arguement. Note you can put your data into $data() array and pass it to view like so, first put data in $data array, $data['some_info] = $information; then pass it to view, $this->load->view('page_name', $some_info), and then in view file 'page_name' you can show that variable's value like echo $some_info.So here's my controller, that takes runs data controller, username method in it and passes it brown argument. Then we pass that brown argument to view to display it. Not that in real sanario we'd also get user info off data base. But since it's just 2nd lesson we'll just pass users' name to view. http://localhost/index.php/data/username/brown.

Lets back off a little and see how we can just use http://localhost/user/jon and still get this working. Switch to routes.php file scroll down and delete pre existing entries. They're at the very bottom after all the commentary. They look like $route['default_controller'] = 'welcome.php'; Delete that too lets create an entry. Note that codeigniter uses *wildcard symbols or keywords for url matching. For example, (:any) means any URL. jon/brown will mean one or another in URL.. if any of it matches, direct user to specified URL.
Our route will look like this $route['user/(:any)'] = "data/username/$1"; It says, whenever user types user/ANY_USERNAME redirect user to data/username/$1 $1 being the argument's value. When user is directed to that page, our controller takes over and process the information showing us the view page.
This is really important content used in almost all frameworks, being as important as it is it requires quite a bit of practice. Best way to practice is to look at different website URL, and make them user friendly using routes.
In next tutorial we'll be learning about model.