PIP is a tiny PHP application framework built for people who use a LAMP stack. PIP aims to be as simple as possible to set up and use.
You can download the latest version of PIP in either zip or tar formats.
You can also clone the project with Git by running:
$ git clone git://github.com/gilbitron/PIP
application/config/config.php
and fill in your base_url
base_url
and hopefully see a welcome message.PIP is very similar in it's architecture to CodeIgniter and will likely look very familiar to you if you already have experince with CodeIgniter. However remember that PIP is a tiny framework and does not include a lot of the functionality that comes with CodeIgniter.
PIP is based on the Model-View-Controller development pattern. MVC is a software approach that separates application logic from presentation. In practice, it permits your web pages to contain minimal scripting since the presentation is separate from the PHP scripting.
With MVC in mind the physical layout of PIP is fairly simple. Your application specific files go in the "application" folder (you don't need to touch the system folder). Inside the application folder there are folders for all of the specific application entities:
When PIP loads files it assumes they are in the corresponding folders. So make sure you place your files in the correct folders.
We encourage you to use the "static" folder in the root to store you static resource files (CSS, JS etc) however you can put them
anywhere. You can also use the BASE_URL
variable to help include files in your HTML. For example:
<link rel="stylesheet" href="<?php echo BASE_URL; ?>static/css/style.css" type="text/css" media="screen" />
All classes in PIP use PascalCase naming. The associated file names must be
the same except all lower case. So for example the class ThisIsAClass
would have the filename thisisaclass.php
.
Underscores in classes must be included in file names as well.
By default, URLs in PIP are designed to be search-engine and human friendly. Rather than using the standard "query string" approach to URLs that is synonymous with dynamic systems, PIP uses a segment-based approach:
example.com/class/function/param
By default index.php
is hidden in the URL. This is done using the .htaccess
file
in the root directory.
Controllers are the driving force of a PIP application. As you can see from the URL structure, segments of the URL are mapped to a class and function. These classes are controllers stored in the "application/controller" directory. So for example the URL...
example.com/auth/login
...would map to the following Controller with the filename auth.php
:
<?php
class Auth extends Controller {
function index()
{
// This is the default function (i.e. no function is set in the URL)
}
function login()
{
echo 'Hello World!';
}
}
?>
...and the output would be "Hello World!".
The default controller and error controller can be set in application/config/config.php
Note that if you need to declare a contructor you must also call the parent constructor like:
<?php
class Example extends Controller {
public function __construct()
{
parent::__construct();
// Your own constructor code
}
}
?>
There are several helper functions that can also be used in controllers. Most of these functions take the parameter $name
of the corresponding class:
loadModel($name)
- Load a modelloadView($name)
- Load a viewloadPlugin($name)
- Load a pluginloadHelper($name)
- Load a helperredirect($location)
- Redirect to a page without having to include the base URL. E.g:
$this->redirect('some_class/some_function');
In PIP a view is simply a web page. They can contain everything a normal web page would include. Views are almost
always loaded by Controllers. So for example if you had a view called main_view.php
that
contained the following HTML:
<html>
<head>
<title>My Site</title>
</head>
<body>
<h1>Welcome to my Site!</h1>
</body>
</html>
... you would load it in a controller by doing the following:
<?php
class Main extends Controller {
function index()
{
$template = $this->loadView('main_view');
$template->render();
}
}
?>
The View class has a helper function called set($key, $val)
that allows you to pass variables from the Controller to the View.
$template = $this->loadView('main_view');
$template->set('someval', 200);
$template->render();
...then in the view you could do:
<?php echo $someval; ?>
...and the output would be 200
. Any kind of PHP variable can be passed to a view in this way.
In PIP models are classes whose sole responsiblity it is to deal with data (usually from a database). For example:
<?php
class Example_model extends Model {
public function getSomething($id)
{
$id = $this->escapeString($id);
$result = $this->query('SELECT * FROM something WHERE id="'. $id .'"');
return $result;
}
}
?>
...then in a controller you would do:
function index()
{
$example = $this->loadModel('Example_model');
$something = $example->getSomething($id);
$template = $this->loadView('main_view');
$template->set('someval', $something);
$template->render();
}
Now the results of your database query would be available in your view in $someval
.
Note that in the above code there are no steps to connect to the database. That is because this is done automatically. For this
to work you must provide your database details in config.php
:
$config['db_host'] = ''; // Database host (e.g. localhost)
$config['db_name'] = ''; // Database name
$config['db_username'] = ''; // Database username
$config['db_password'] = ''; // Database password
There are several helper functions that can also be used in models:
query($query)
- Returns an array of results from a queryexecute($query)
- Returns the direct result from a queryescapeString($string)
- Escape strings before using them in queriesThere are two types of additional resources you can use in PIP.
Session_helper
and Url_helper
) which are examples of how to use helpers.Thanks to the following people for contributing to PIP:
v0.5.3 - Several changes:
Url_helper
segment
function.query
would add an empty element to the end of the result array.model.php
.v0.5.2 - Several changes:
RewriteBase
in .htaccess
.pip.php
.style.css
from Views folder to static folder and updated header.php
.v0.5.1 - Several changes:
BASE_URL
define.v0.5 - Initial version.