By Alvie Amar | 7/13/2016 | General |Beginners

What is PHPixie?

What is PHPixie?

PHPixie is a lightweight, open source PHP web application framework that uses a straightforward implementation of the request-response flow yet still allows for the use of more complex models, for example, HMVC. PHPixie handles many things in a different manner from the full-stack frameworks making it a good option for small scale web apps and projects.

 

PHPixie's simple architecture and small size give it the fast performance that developers want for smaller projects and it's ideal for systems with limited resources such as those on a VPS. Further, PHPixie's code replaces functions that are process-intensive, e.g. regular expression, with more lightweight code.

 

PHPixie began in mid-2012 as a fork of the Kohana framework and was designed for simple read-only websites whose focus was on performance. In July 2012 the 1.0 version was released having been written from the ground up due to Kohana's core begin mostly patches.

 

Here are some of the key features of PHPixie framework:

  • Package system
  • Direct code flow
  • MongoDB support
  • Use of best practices
  • Solidified security
  • Direct support

 

What is PHPixie good for?

 

There are lots of frameworks available in the market so why should you use PHPixie as your PHP framework? Well, PHPixie gives more adaptability, as everything is effortlessly over-ridable, expandable and simple to learn. Actually, it is exceptionally helpful on the grounds that you will be responsible for what is going on in building your application. Besides that, PHPixie is useful as a part of any application due to its straightforward execution of the request-response flow. Also as opposed to many other frameworks that load class names with an auto loader, PHPixie does not force a naming convention leading to better readability.

 

These are some of the advantages of PHPixie framework:

  • Concentrate on speed and page load time.
  • Focusing on giving a stable and adaptable architecture.
  • As opposed to other lightweight frameworks that do not give a full stack of features, PHPixie provides a restricted full stack that focuses on effectiveness.
  • PHPixie should be easy to learn and understandable to anyone with experience in Codeigniter or Kohana.
  • Provides a stable codebase that doesn't "get in the way" and allows the developer to write their own code how they see fit.

 

Example and code snippets

 

To explain the essentials of creating applications with the PHPixie framework we will make a little Poll application. Before the end of this instructional exercise, you will have a working survey application using PHPixie.

 

Before going on, ensure you download PHPixie from the project's site and extract the downloaded archive into the root web directory of your web server. Visit http://localhost/(or different location as proper) in your program and you ought to see the ‘Have fun coding!’ welcome message.

 

Simply follow the steps below for speedier inquiries.

 

Step one: Let's make our database

 

Initially, we will have to make two tables in the database: one we'll call “polls” which will store the questions, and “options” that will contain possible responses for each survey and the number of votes that each one got.

 

Open up your MySQL and run these queries:

 

CREATE TABLE polls (
   id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
   topic VARCHAR(255) NOT NULL,
   PRIMARY KEY (id)
);

CREATE TABLE options (
   id INTEGER NOT NULL AUTO_INCREMENT,
   name VARCHAR(255) DEFAULT NULL,
   poll_id INTEGER UNSIGNED NOT NULL,
   votes INTEGER NOT NULL DEFAULT 0,
   PRIMARY KEY (id),
   FOREIGN KEY (poll_id) REFERENCES polls(id)
);


To configure the database connection, we alter the database.php file inside the application/config directory: 

 

 

<?php

return array(

   'default' => array(

       'user' => 'dbuser',

       'password' => 'dbpassword',

       'driver' => 'pdo',

       // 'Connection' is required if we use the PDO driver

       'connection' => 'mysql:host=localhost;dbname=polls',

       // 'db' and 'host' are required if we use Mysql driver

       'db' => 'polls',

       'host' => 'localhost'

   )

);

 

PHPixie can be configured to utilize numerous database connections, however since we are working with the single database we determine it as default. That is all the configuration required, so we can move on to writing the code now.

 

Step two: Let’s take a look at our model

 

Commonly, models handle operations like recovering data from the database and saving it. PHPixie deals with these operations utilizing an ORM layer so we do not have to compose any database queries at all. We simply need to characterize our models this way:

 

<?php

class Poll_Model extends ORM {

   // Each poll can have many options

   public $has_many = array('options');

   // This way we can define some additional

   // dynamic properties for a model.

   // Later on we will be able to access it via $poll->total_votes

   public function get($property) {

       if ($property == 'total_votes') {

           $total = 0;

           foreach ($this->options->find_all() as $option) {

               $total += $option->votes;

           }

           return $total;

       }

   }

}

 

The above code should be placed in application/classes/model/poll.php.

 

 

<?php

class Option_Model extends ORM {

   public $belongs_to = array('poll');

   public function get($property) {

       if ($property == 'percent') {

           if ($this->poll->total_votes == 0) {

               return 0;

           }

           return floor($this->votes / $this->poll->total_votes * 100);

       }

   }

}

 

The code for the options model is placed in application/classes/model/option.php:

 

Step three: The Controller

 

Every page must have an activity characterized for it inside the controller and we will require an HTML format for each page as well. Along these lines, a basic Controller that just shows the formats would appear like this:

 

 

<?php

class Polls_Controller extends Controller

{

   public function action_index() {

       // This is how we load up a template

       $view = View::get('index');

       $this->response->body = $view->render();

   }

 

   public function action_poll() {

       $view = View::get('poll');

       $this->response->body = $view->render();

   }

 

   public function action_add() {

       $view = View::get('add');

       $this->response->body = $view->render();

   }

}<?php

class Polls_Controller extends Controller

{

   public function action_index() {

       // This is how we load up a template

       $view = View::get('index');

       $this->response->body = $view->render();

   }

 

   public function action_poll() {

       $view = View::get('poll');

       $this->response->body = $view->render();

   }

 

   public function action_add() {

       $view = View::get('add');

       $this->response->body = $view->render();

   }

}

 

Now our controller will look like this (save the following as application/classes/controller/polls.php):

 

 

<?php

class Polls_Controller extends Controller

{

   protected $view;

 

   public function before() {

       // We load up the main view and

       $this->view = View::get('main');

 

       // Now we find a full path to a view that has

       // the same names as the action to be excuted

       $template = Misc::find_file('views', $this->request->param('action'));

 

       // We pass the view we located to our main template.

       // All properties assigned to a view will be available

       // as variables inside the template

       $this->view->template = $template;

   }

 

   public function after() {

       // After an action completes we render the view

       $this->response->body = $this->view->render();

  }

 

  public function action_index(){

       $view = View::get('index');

       $this->response->body = $view->render();

   }

 

   public function action_poll() {

       $view = View::get('poll');

       $this->response->body = $view->render();

   }

 

   public function action_add() {

       $view = View::get('add');

       $this->response->body = $view->render();

   }

}

 

Step four: HTML Layouts Using Views

 

Views are fundamentally HTML files with embedded PHP code to show the qualities appointed to place-holder variables from inside the controller. Our principal view should simply exhibit some regular HTML and incorporate a correct sub-template.

 

The following is application/views/main.php:

 

 

<!DOCTYPE html>

<html>

<head>

 <title>PHPixie polls</title>

 <link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" rel="stylesheet">

 <link href="/style.css" rel="stylesheet">

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>

</head>

<body>

 <div class="container">

  <div class="span4"></div>

  <div class="span4">

   <h2>PHPixie Polls</h2>

   <!-- Here is where we include a subtemplate -->

<?php include($template);?>

  </div>

  <div class="span4"></div>

 </div>

</body>

</html>

 

Step five: Creating Individual Pages

The polls page ought to simply display a rundown of accessible polls. It just requires a slight change to controller/polls.php.

 

 

<?php

//...

public function action_index() {

   // We pass all stored polls to the view

   $this->view->polls = ORM::factory('poll')->find_all();

}

 

The view of the page will be a straightforward rundown with a connection to the poll creation page. The following is code for views/index.php:

 

 

<ul class="nav nav-tabs nav-stacked">

<?php

foreach($polls as $poll) {

?>

<li>

 <!-- This is how a link to a single poll will look like -->

 <a href="/polls/poll/<?=$poll->id;?>"><?=$poll->topic;?>

  <div class="muted"><?=$poll->total_votes; ?> Votes</div>

 </a>

</li>

<?php

}

?>

</ul>

<a class="btn btn-block" href="/polls/add"><i class="icon-plus"></i> Add a Poll</a>

 

If we somehow managed to add a little data to the database now we would see the following while accessing to http://localhost/polls/index (list can be excluded from the URL as it is the default activity):

 

This is just a simple Poll application to demonstrate the basics on how PHPixie handles things and how easy it is to develop with it. If you would like to explore further you can find it on Github. 

 

Conclusion

PHPixie began as a smaller scale framework and became a standout amongst the most prominent full stack PHP frameworks. This is thanks in part to its strict architecture that helps avoid common problems such as dependence on static methods, global scope, singletons and other antipatterns, making for readable, extendable, and testable code. Further, all PHPixie components boast full unit test coverage. It is the PHP framework that will never get in your way while giving the developer full control–yet it is still easy to learn.

 

Just as important the PHPixie community is friendly, active and supportive so you can anticipate getting an answers to your inquiries within minutes. The framework documentation is brimming with illustrations and is regularly updated with instructional exercises. So take some time and get to know this modern framework by going to the PHPixie website. Have fun learning!

By Alvie Amar | 7/13/2016 | General

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Recent Stories

Top DiscoverSDK Experts

User photo
3355
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
2340
Meir Rabinovich
Real time and embedded developer
Hardware and RT | General Libraries and 5 more
View Profile
User photo
1540
Nathan Gordon
Full Stack developer
Web | JavaScript and 1 more
View Profile
Show All
X

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.

{{compareToolModel.Error}}

Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now