Hello Everyone,
I wrote this script way back in 2008 as Version1. However, I was forced to forget about this, because it was criticized by many developers with BS, MS, and "Seasoned PHP Developer" under their names. During those days, I couldn't stand up for my own reasons, because I can't even put High School graduate as my lone achievement under my name. I was told this script was 10 steps backward to where we at in 2008. If my memory still served me well, even my own brothers in Silicon Valley have asked me to forget about this.
enough of bad memories behind this script Honestly, I could have developed this into something useful, if I was given just a little moral support.
CLI was introduced in PHP some very long time ago. However, it wasn't popular and most of the people have questioned or were not interested in writing any application VIA the command line Iterface. For some odd reasons, laravel, FuelPHP and others took advantage of the CLI features. Anyone who are familiar with Laravel, Symfony2, and Fuel PHP can easily relate to what I am talking about. Else, my sincere apology for bringing the subject on some relic technology.
I love the idea of being able to generate or create an application through command prompt. This may sound too foolish and Backward in 2008, but today I can easily demonstrate why it is important for new comers in PHP MVC Framework, most imporatantly CodeIgniter users.
Why Do it on CI?
Believe it or not, CI played a major role in the innovation of many PHP MVC frameworks. Although it may have been ignored by others, we cannot deny how important CodeIgniter's contributions to PHP MVC framework developers. CI is the most lightweight Framework out there, it is also the most extensible and the easiest to modify.
What this script is all about?
This script was intended to serve as a training tool in helping new developers and students to write easily create simple application on CI.
Requirements?
1. XAMPP or Uniform Server
2. CodeIgniter.
Few Steps to keep you going
Step One: Open your command prompt, CD to xampp/ or zero/ (for uniform server) and then type
php -v
you should be getting a response something like this
C:\public_directory>php -v
PHP 5.5.3 (cli) (built: Aug 20 2013 16:11:53)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
If you are reading resposne similar to the above, you are good to go.
Step Two: Unzipped CodeIgniter to htdocs directory or www directory for uniform server.
Step Three: load application/config/config.php on your favorite source code editor e.g. notepad++.
find line 47 or this line of codes
$config['uri_protocol'] = 'AUTO';
right below it add
$config['uri_protocol'] = isset($_SERVER['REQUEST_URI']) ? 'PATH_INFO' : 'CLI';
save your changes...
Step Four: Copy the snippets I have provided above and save as application/controllers/build.php
Step Five: Type this on your command prompt
php index.php build create_app myapp
Step Six: direct your favorite browser to localhost/codeIgniterLocation/myapp. You should see something like this
This is my first application
This is your first application
Explore and Learn
The snippet above created these files for you
application/myapp.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Myapp extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index(){
$this->load->model('Myapp_model');
$data['content'] = $this->Myapp_model->set_index();
$this->load->view('Myapp_view', $data);
}
}
?>
the model file
application/models/myapp_model.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Myapp_Model extends CI_Model {
public function __construct(){
parent::__construct();
}
public function set_index(){
return ('This is your first application');
}
}
?>
Lastly, the snippet will create the template file application/views/myapp_view.php
<html>
<title>Myapp3 view page </title>
<body><h2> This is my first application</h2><?php echo $content; ?></body>
</html>
Experiment for all the possibilities. The snippet above have given you the basic foundation of creating an application in CI without trying to figure out how its done. Just follow the snippet's trail and you won't get lost.
What's Next
I might write another tutorial about CI sometime this year. If you noticed this part of the snippet?
public function create_app($controller,$migration=false)
That migration, if set to true, we can make the CI to generate a file called migration for automatic database creation. I just don't feel like covering the subject about migration, because it will confusing to some. Please come back here in Daniweb often and check tutorial and snippet sections.