Welcome Guest, Not a member yet? Register   Sign In
Help meeee - is this feasible for codeigniter implementation
#1

[eluser]Subclass[/eluser]
Hi All - First Post,

I've been going mental with my PHP development, and seriously feel like I need a better solution as I'm coding it new from scratch almost every time and it's draining my time and efforts.
Was wondering if this kind of approach is feasible for code igniter - only slight issue is that I currently rewrite all requests through index.php allowing me to have any url for pages

I'm basically splitting my application into 4x parts

1. Objects - basically properties and a constructor (no clever stuff)
2. Wiring - classes to construct objects (run db query, get data, create 'new' objects & return)
3. Templates - no php code here, just placeholders
4. Index - .htaccess rewrites all requests (except images/robots.txt/admin/etc)

Problems I have (why I haven't put it into CI earlier) - have the requirement for custom urls - not /1/2/3 architecture

Lack of understanding


Objects
--------

page.php

Code:
class Page
{
public $id;      // autogen from mysql
public $name; public $url; public $seotitle; public $seodescription
....

public function __construct
(
$id,$name,$url,$seotitle,$seodescription,....
)
{
$this->id = $id; $this->name = $name; ....
}
}

Wiring
-------

pages.php

Code:
class Pages
{
static function get_page($id)
{
$query = "SELECT * FROM `pages` WHERE `id` = {$id}";
$res = mysql_query($query);
$row = mysql_fetch_object($res);

switch($row->type)
{
  case "homepage":
  return new homepage($row->id, $row->name, $row->url ....);
  break;
  case "page":
  return new page($row->id, $row->name, $row->url ...);
}
}
static function get_all_type_pages($type)
{
  // select * from pages where `type`= $type
  // return array of page objects
}
}


Templates
-----------
basically html files with placeholders %field1%, %field2%, <title>%seotitle</title> etc


Main File (index.php)
----------------------------
Code:
require(settings.php); // domain, mysql, constants etc
require(pages.php);

// get page id
$request = $_SERVER['REQUEST_URI'];
$query = "SELECT * FROM `pages` WHERE BINARY `url` = '{$request}'";
$res = mysql_query($query);
if(mysql_num_rows($res) < 1)
{
//throw 404/intelligent sitemap
exit();
}
$page = Pages::get_page($id);

switch(instanceOf $page)
{
    case "homepage":
        $page['content1'] = $page->content1;
        $page['content2'] = $page->content2;
        $output = file_get_contents("templates/homepage");
        break;
}

foreach(array_keys($page) as $key)
{
   $output = preg_replace("/%$key%/",$page[$key],$output);
}
echo $output;

Any feedback much appreciated ???
#2

[eluser]Phil Sturgeon[/eluser]
Your "Objects" seem to do the job of controllers and libraries, your "Wiring" is models and your "Templates" are Views.

Congratulations, you use the MVC architecture! Smile

Check out the user guide to see how CodeIgniter does this all. It will stop you re-inventing the wheel and let you spend your time using the shinier wheel that is CodeIgniter!
#3

[eluser]Subclass[/eluser]
Thank you cookie-monster-type-thing

I'll get cracking on it, just wondered if I was (and apparently I am) recreating an existing framework as have coded this from scratch over around a year.
#4

[eluser]Phil Sturgeon[/eluser]
I'm sure it was all good practise. Any time spent doing high-level stuff like that is time well spent. I'm pretty sure my baby (PyroCMS) will be fairly useless when EE 2.0 comes out, but not going to let that stop me. Smile
#5

[eluser]Subclass[/eluser]
ee2?
#6

[eluser]Dam1an[/eluser]
EE2 = Expression Engine 2, it's the commercial product made by Ellis Labs




Theme © iAndrew 2016 - Forum software by © MyBB