How to add Bonfire to an existing Codeigniter framework project

MISSION: Integrate simple codeigniter project https://github.com/martinberlin/onstage
with bonfire powerfull HMVC modules and administration access.

Step 1

Copy application and bonfire folders into project root.

 

Step 2

Modify existing MY_loader class that will be overridden by Step 1 with your custom functions. In my case, I was using sparks library and the existing codeigniter theme functionality. So I left :

application/core

/* load the MX_Loader class */
require APPPATH.”third_party/MX/Loader.php”;

/* This will be class MY_Loader extends CI_Loader {}  in your Codeigniter project */

class MY_Loader extends MX_Loader {

// And copied here my custom methods.

}

Step 3

Routing and class names:

http://onstage/public/index.php/event/hamburg/215  (Local test)
Fatal error: Cannot redeclare class Events in C:\xampp\htdocs\onstage\bonfire\libraries\events.php on line 33

Small fail: It seems bonfire already has a class called events. And collides with my own events controller…So I will rename it to something else and change routing accordingly.

So, we update the route:
$route['event/(:any)/(:num)'] = “eventos/lookup/$1/$2″;
And the corresponding file controllers/events.php to eventos.php . This is the beautiful part of MVC. We just changed some files and routing, but the URL call will be exactly the same. This internal changes won’t affect at all any frontend functionality.

Ok and now the latest routing update, this time in the apache virtual host, the codeigniter goes to the root. But the Bonfire default is the public directory.

With this change, so far, almost all old codeigniter project is working like before, with the added plus that now has a Bonfire administration. But we have still some routes to correct.
For example, the old city route is no longer working:

http://onstagekonzerte.de/hamburg/

Drops a 404. But the application/config/routes.php are there :
$route['hamburg'] = “home/hamburg”;
$route['munich']    = “home/munich”;

And the home controller has those methods inside. So I yet have to find those.

UPDATE:

The solution to my path problem was solved in two steps:

a)  First changing the application/config.php and removing the index.php page from the index_page variable:
$config['index_page'] = ”;

b)  Modifying the view paths to css / js external HTML resources, referring to step 2 since bonfire goes to /public as default:

<link rel=”stylesheet” href=”/public/css/alpha_background.css”>  So now new route will be :
<link rel=”stylesheet” href=”css/alpha_background.css”>

After this I had http://onstagekonzerte.de running in the front exactly as before, with the benefit of having a complete bonfire admin in the back-end.

I’ve posted this in the bonfire forum to see if I can get some feedback and also to share with others, since this is a completely open project where all changes will be shared on a github account where anyone can fork, edit, and comment on changes.

onstagekonzert.de project update

OnStage Konzerte

https://github.com/martinberlin/onstage/commits/master 

Apr 12, 2014

  1. Martin

    Adding import route

  2. Martin

    Finished module last.fm import + added spark tools

Tricky Php questions where I failed doing a self test before the PHP Certification

Preparing for the exam in a few hours. There are many functions I never used used in the latest 6 years, and that’s all fine, since PHP is quite a bloated language. So I never had the need to use things as mysql_real_escape_shit :)

Or ucword ? WTF is that ? Anyways now learning the latest tidbits, and so far got 41 questions right from 70 which is a lame 5,5 and for sure won’t get approved if I rush on to get it done on time. So I will take it slowly.

#1 Which of the following is a valid way to pass the $callback parameter expected by array_walk()? 

1) An anonymous function
2) An array containing an instantiated object as the first element, and
the method name as the second element
3) A string containing the function name
This question is about callbacks – only these three items in the list are valid ways to specify a callback

I will make a short example of the #2:

<!–?php

class myobj{

   static function test($v){
     echo $v;
   }
}

$arr=array(1,2,3,4);

array_walk($arr,array(“myobj”,”test”));

 

// This is the right way to pass the object and the method that will be executed per array item.

// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj, ‘myCallbackMethod’));

more types in the php docs.

// Method is called statically so it should be declared that way or you get a warning.

#2 Right way to parse a web page at http://onstagekonzerte.de as XML?

DOMDocument::loadHTML(file_get_contents(‘http://onstagekonzerte.de&#8217;));

 

#3 A brief explanation about bitwise operators

$b= 4<< 2 + 2;

First of all, via operator precedence 2+2 is executed first resulting in:

$b= 4<< 4;

<< left shit, means that it's multiplied 2 ala n, so in this case: 4 *2*2*2*2 = 64

>> right shift would mean divide, so :  64 >> 4 = 4

#4 The standard PDO fetch by default

Does it return an:
  • Associative Array
  • Numeric Array
  • or Both ?

Leave your answer in the comments :)

Another questions about PDO query, ask about what it performs. Query in PDO does two things:
prepare() and execute(). Only that! Don’t be tempted to add a fetch, that you do afterwards, lazy programmer.

#5 The PCRE tricky question

$pattern= ‘/[a-z]{4} /’;

$string = ‘Mary was a little bitch’;

$matches = preg_match( $pattern, $string );

print_r($matches);
~

There is always some tricky question like this. So what returns this ?

A 1.    Not the matches, to get those $matches should be added in as a third parameter.  But in this way, preg_match just returns true if the match is found, which in this case would be ‘ttle ‘ since bitch won’t do it since it’s missing a space at the end. Bitchy, doesn’t it ?

#5b The PCRE tricky question in bitch mode

$pattern= ‘# \w*?[aeiou]{2}\w*? #’;

$string = “There’s a lion loose in the Maister park”;

$matches= array();

preg_match_all($pattern, $string, $matches);

First of all, the # delimiters are valid delimiters, the only requirement is that whatever you pick to match as the start should be matched at the end.  Note that as ” lion ” is matched, eats the space in “loose ” so loose will NOT match. And there you may be trapped.

There’s a lion loose in the Maister park
Array (
[0] => Array (
[0] => lion
[1] => Maister
)
)

#6 The HTTP status questions

Everybody and his mum, know about 404 not found.  But in this exam there come another statuses as:

  • 401  – Not authorised, put your user and password in this little box
  • 400 - Bad Request
  • 403 – Forbidden
  • 408 - Request Timeout

#7 The PHP magic methods

Remember the magic list:

  • __construct
  • __destruct
  • __call
  • __callStatic
  • __get
  • __set
  • __isset
  • __unset
  • __sleep
  • __wakeup
  • __toString
  • __invoke
  • __set_state
  • __clone       Some questions may ask what magic methods is instanced when you clone a class. Well it’s pretty obvious, don’t get lost there, is this one.

Analog B&W scan done today:
Image

Codeigniter MVC #1

I though about publishing some codeigniter series, just to save some steps in my memory and also to share it with other people that want’s to start with MVC. I’ve done some Model-View-Controller in the past but was time ago and with Zend 1.

So the mission is simple, I will take this events site that I made in 3 hours on holidays, and rebuilt it using codeigniter. I will try to be as methodic as possible and do regular commits on this public github.

So in this commit we will update the config file, to autoload some libraries, change the default route and connect to our database:
git commit -m 'initial configuration + public/lyve.sql db structure'
modified: .htaccess
modified: application/config/autoload.php
modified: application/config/config.php
modified: application/config/database.php
modified: application/config/routes.php
modified: application/controllers/home.php

Result of 1st weekend of self-taught MVC ist online, if you are interested take a look:

http://onstagekonzerte.de

and browse the source. Please leave comments if you are going to clone this one.

Looking for the most usable Jquery dashboard

I have something in mind to create, but I need an essencial piece, that I still didn’t found, or better than that: It does not exist. It needs to be created.

For dashboard I mean some modular thing with gadgets, like of the old iGoogle, but better and focused in data. So for ex. using datatables for the lists and highcharts for doing some fancy graphics.  And all this should save state in each action, so you can create a complete dashboard with your data, log in and log out, delete cookies and still have the same configuration.

In the process of checking what is existing already I come upon this ones, each one with it’s coolness and deficiencies.

sDashboard

  1. sDashboard

    • Built in support for rendering datatable’s and flotr2 charts
    • Support to listen for events such as table row click, chart data click and data selection built-in
    • Works in mobile, so you can drag and drop the thingies, looks quite fit in my Nexus
    • So far, this is the winning candidate, although I was looking forward something with a bit more Layout flexibility

    Some additional features:

      • Depends on jQuery, jQuery UI, datatables, Flotr2 charts
      • Has 8 options, 4 methods and 7 events to customize
  2. Jquery layout

    Although not a dashboard system, jquery layout is an interesting tool to build a dynamic layout that can be resized and contain widgets inside.
    The layout saves states in json, but has no ability to store widgets order, so that has to be built on the top.

  3. Ext JS
    This library also has high chances to become a good dashboard library.
    But at the moment I favour more a Jquery solution. Although trying out Ext JS is not discarded. I’ve worked with the library some years ago and it’s a very nice framework to work with.

So what is your favourite tool to build a customizable dashboard ? Comment below :)

PHP Certification studies part 2: Data Formats

Although Data Formats has “Low emphasis” in the test, it’s quite important for you as a developer. In the latest year, every major website has API Access, and there about 5000 + of them listed on programmableweb only in Json format. Understanding Data Formats then it’s quite a must if you want to build something that “Talks” to other platforms.

Sections

  • XML  Extensible Markup Language
    1 xml_parser_create() type constructor, to parse difficult XML data, like openimages Xml api
    2 simplexml_* functions to work with simple Xml data from php
    3 DOM permits manipulating XML documents
    4 xpath() Method to perform queries agains XML.
    Can work with simplexml FORMAT: array SimpleXMLElement::xpath(string $path)
  • SOAP Simple Object Access Protocol
    This Php extension is used to write Soap Servers and Clients
    They are runtime configuration variables and predefined  constants that start with SOAP_*
  • REST REpresentational State Transfer
    All the web is a giant REST model. Conceptually is a network of pages, accessed by users through a set of Links. Uses HTTP “verbs” :
    GET  List (no id) /Resource (needs an id)
    POST Create
    PUT  Update (needs an id)
    DELETE delete (needs an id)
  • JSON Javascript Object NotationJson is an data-interchange format. Is mostly used to pass data from Php to javascript and the extension is loaded in Php by default.
    The functions start with json_* being the most used: json_decode($json) & json_encode

Date and time

The date functions retrieve the date and time where the php server is running.

REFERENCES:

http://us2.php.net/manual/en/book.xml.php

http://us2.php.net/manual/en/book.dom.php

http://php.net/manual/en/refs.webservice.php

http://us.php.net/manual/en/book.json.php

http://php.net/datetime

http://php.net/simplexml

http://php.net/domxpath

http://php.net/soap