Tuesday, September 17, 2013

Creating A Simple PHP MVC Framework Part 6A, First Application- Youtube API (smarty version)

Written by Lorenzo D. Alipio (open source developer)  

 
We are now in part 6A of this tutorial series, and I am pretty excited to write a simple application for our simple PHP MVC framework. After writing this simple application, you will probably agree with me that not all applications should be built upon the much bigger frameworks such as CodeIgniter, Kohana, Zend, Symfony. I will be writing the same application in Kohana  and CodeIgniter in the upcoming tutorials. Just to demonstrate how it is done on a bigger frameworks.

Our simple application will not be needing a database, because it is solely based on the Youtube API version 2. I honestly believe that if we can implement it on youtube API, I don't see any reason for us to implement other types of application to be build on this simple framework.

Here is the plan. We will be writing all of the controller, model, view pages first, and then create the template files ustilizing the smarty. This application will have a functional search class, pagination, and play page.

The Controllers we need to create
1. AboutController.php
2. CategoriesController.php
3.IndexController.php
4.LatestController.php
5.MostviewedController.php
6.PlayController.php
7.TopRatedController.php

The Models we need to create
1. Since there is no Database for our first application, we will not be creating any model file.

The Views we need to create
1.ViewClass.php


Filename:  application/controller/AboutController.php
<?php

/*
 * Copyright Lorenzo D. Alipio 
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * AboutController.php
 */



class AboutController extends Controller{

public function __construct(){
 parent::__construct();
 
 }
 
private function aboutContent(){
 return array(
  'about'=> 'Created with L.D.Alipio light MVC, twitter bootstrap, smarty template engine.',
  'email' => 'YourEmail@domain.com',
  'name' => 'Lorenzo Alipio',
  'motto' => 'You can do it (not like the Nike though.)',
  'sitepath'=>SITEPATH
  );
 
} 
public function index(){
 
   return $this->view->set_content(self::aboutContent(),'about.tpl');
 
}
 
}


Since this is the very first functional controller of our application, I would like to take this opportunity to explain the basics of what, why, and where all these things are going to go, and I am really hoping I can explain them with clarity.


Please take a look at the codes below



class AboutController extends Controller{

public function __construct(){
 parent::__construct();
 
 }

The AboutController is named after the job or function of this controller. So if we are going to write a controller responsible for member's registration, we can named if as RegisterController.php. We should always develop some form of naming convention for the purpose of clarity and quick identification of our classes and files.

Besides from establishing our  naming convention, the class above extends the base controller class from our core directory, and this class will also inherit the parents constructor. Mainly, the instantiation of the base model and view classes. Remember in the earlier part of this tutorial. The constructor of our base controller looks like this

 public function __construct(){
 $this->model = Ini_object::instance('ModelClass');

$this->view = Ini_object::instance('View');


 } 

Second is the method in our class called private function AboutContent, again we can easily noticed that even the naming of our method within the class gives us the cue of its function. In this particular class, the method AboutContent is going to deliver the content of this page. This content could be from database or from just a simple array of text strings. The visibility of this method is set to private, because if we are to use database as the prime source of our content, then we don't have to make an adjustments, it is already set-up as it should.

Third method is the fulfillment of the agreement between the parent class and all of its child classes. Remember in our core controller file, we declared  abstract function index();   by declaring function index() as abstract, all of the child class must fulfill this or must have a function called index().

Noticed that the method index() assigned the output of the aboutcontent method? This is not just a simple assignment though, if we can recall what we just did in previous part, we incorporated Smarty template engine. We will be writing the ViewClass.php to process and pass the output to our template file.

I hope you are understanding concept here, because if you will be venturing in a much bigger framework like CI or Kohana, you will be using similar approach in writing the application's controller. You will see more of this in CI and Kohana tutorials.

Let's move on..

Filename: CategoriesController.php
<?php

/*
   * Copyright Lorenzo D. Alipio 
   * This file is for demonstration purposes only and NOT authorize for distribution.
   * NO License will be granted for any purposes NON-profit or for profit.
   * However, the instance of smarty within the constructor of which this class instantiated is covered
   * by GNU license.
   *
   * filename: application/ViewClass.php
   * Update Date: 8-20-2013
   * Previous Version: 1.4
   * CategoriesController.php
*/

class CategoriesController extends Controller{

public function __construct(){
  parent::__construct();
 
 }

 public function index(){
 
  $cat = array(
'Charice','The Beatles','Elvis Presley', 'Nora Jones', 'James Brown','Rolling Stones','Bob Dylan','Chuck Berry','The Who','Led Zeppelin','Stevie Wonder','Jimi Hendrix','Ray Charles','The Beach Boys','Pink Floyd','Aretha Franklin','Little Richard','Marvin Gaye','Bruce Springsteen','David Bowie','Fats Domino','Black Sabbath','Queen','Buddy Holly','Bob Marley','Sam Cooke','Elton John','Neil Young','U2','Run-D.M.C.','Bo Diddley','Jerry Lee Lewis','B.B. King','Sly & The Family Stone','The Clash','Prince','The Grateful Dead','The Velvet Underground','Nirvana','Michael Jackson','The Supremes','The Temptations','Madonna','Public Enemy','The Kinks','Otis Redding','The Everly Brothers','Van Halen','Elvis Costello','Simon & Garfunkel','Cream','Frank Zappa','Roy Orbison','Eric Clapton','George Clinton & Parliament/Funkadelic','The Allman Brothers Band','Janis Joplin','The Ramones','Crosby, Stills, & Nash & Young','Fleetwood Mac','AC+DC','The Byrds','Joni Mitchell','The Eagles','Smokey Robinson & The Miracles','R.E.M.','Creedence Clearwater Revival','Johnny Cash','Van Morrison','Aerosmith','Curtis Mayfield & The Impressions','The Drifters','The Police','Metallica','Pearl Jam','Deep Purple','Bill Haley & His Comets','The Band','Santana','Yes','Jefferson Airplane','King Crimson','Al Green','The Isley Brothers','The Moody Blues','The Sex Pistols','N.W.A','Talking Heads','Tom Petty & The Heartbreakers','Billy Joel','The Bee Gees','The Yardbirds','The Four Tops','Radiohead','Patti Smith','Guns N Roses','Chicago','Rush','Bob Seger & The Silver Bullet Band','Rick Nelson','Earth Wind & Fire'
); sort($cat);
 
  $content = array(
     
     'sitepath'=>SITEPATH,
     'categoryHeader' => 'Music Video Categories',
     'cat'  => $cat
  );

  
  return $this->view->set_content($content,'categories.tpl');
 
 }
 
 }



The CategoriesController above is pretty explanatory, similar to the AboutController.php it is also following our established naming convention. I could not think of anything that we can use as category for our application. So, I figured maybe we can use all the musicians and bands of Rock n' Roll. I hope you don't mind for the sake of learning. Yes, you are feel free to change them after mastering this subject matter. For now, just leave it like that. You will have plenty of time experimenting it for your own application. I honestly believe music is universal so this will not create some issues ( I keeping my finger cross).

Disclaimer! If you are the owner of any copyrighted materials from youtube, or you are the band, or the performer, or the singer, or the artist/s, and is/are included in the random categories, I picked up from youtube, and for any reason you want your name or band remove from the list, please let me know and I will gladly remove it from the list. While you are already crusading for removal requests, please do it on youtube also, So that there will be no search results of your Name or Band on Youtube search API. All copyrights, trademarks, service-marks and everything in between  belongs to its rightful owner.


I apologize for a lengthy disclaimer. We are programmers and not lawyers, so let's move on to the next file. If you have any suggestions on what category should be use, please let me know. As long as we can utilize pagination for the result.

Filename:  IndexController.php
<?php

/*
* 
* Copyright Lorenzo D. Alipio 
* 
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
* 
* IndexController.php
*/


class IndexController extends Controller{

public function __construct(){
  parent::__construct();
 
 }

 public function index(){
  $content = array(
     
     'sitepath'=>SITEPATH,
     'content' =>self::article()
  );

  ## this model content is for database
   //return $this->view->set_content($this->model->Content(),'index.tpl');
  
  return $this->view->set_content($content,((TPL === 'Smarty')? 'index.tpl' : 'index.html'));
 
 }

 private function article(){
  $article =array(

   'first_p'=> 'This demo application script demonstrate the power of MVC methodologies in web development. 
    This demo application also demonstrate the implementation of the capabilities of the following:',
             'lists' => array('Smarty templating engine','Twitter Bootstrap Front-end framework,Smarty template inheritance','Youtube API version 2,Clean extensible PHP script,Pure OOP as written in Model View Controller design Pattern','Request routing to web or public directory only','Auto redirect to the index, if the page request does not exist','Pages have their own controller to better manage the content delivery','No HTML tags in the business logic pages','Written in PHP 5.5.1 > standards'),
       'more_info'=>'This template may appear to be simple in its form, but it demonstrated the advance implementation
    of Object Oriented PHP.',
        'copyright'=> 'Property of Lorenzo D. Alipio');
    
 

 return $article;
 }
 }





Filename: LatestController.php 

<?php

/*
* 
* Copyright Lorenzo D. Alipio 
* 
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
* 
* LatestController.php
*/


class LatestController extends Controller {

 public $q = "most_recent";
 public $index = '1';
 public $orderby = "published";
 public $i = 48;
 public $count ='';
 function __construct() {
  
  parent::__construct();
  Use_Class::autoload(YOUTUBE);
  
 }

public function index(){

 //$object = Ini_object::instance('YoutubeVideo');
 if(isset($_GET['q'])){
 $q = $_GET['q'];
    $q = preg_replace('/[[:space:]]+/', '/', trim($q));
    $q = str_replace('/', '+', $q);
 $index = $_GET['start-index'];
 $orderby = $_GET['orderby'];
 $i = 25;
}
else{
  $q = "AC+DC";
  $index = "1";
  $orderby = "published";
  $i = 25;
}

$object = new YoutubeVideo($q,$index,$orderby,$i);
 
 $video = array(
     'count'=> $object->getCount(),
     'video'=> $object->getVideo(),
     'sitepath'=>SITEPATH,
     'cat' => $q,
     'pagination'=>$object->pagenate('latest.php')
  );
 
 return $this->view->set_content($video,'latest.tpl');
 
}


}  




This conclude part 6A.. we will be creating the remaining controller files in part 6B.

No comments:

Post a Comment