Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
Moderator: General Moderators
Luke
The Ninja Space Mod
Posts: 6424 Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA
Post
by Luke » Tue Aug 01, 2006 12:49 am
This works really well for a structure like index.php/action/param1/param2/param3, but my problem is now this...
what if the page controller needs something like the registry passed to it??
Code: Select all
<?php
class Front{
private $action;
public function __construct(Registry $registry, $dir='action/'){
$request = $registry->get('Request');
$registry->register('Mapper', null, $request->get('PATH_INFO'));
$mapper = $registry->get('Mapper');
$this->action = $mapper->getAction();
$this->params = $mapper->getParams();
$registry->register($this->action, null);
$registry->setParams($this->action, $mapper->getParams());
$this->object = $registry->get($this->action, $dir);
}
}
?>
Jenk
DevNet Master
Posts: 3587 Joined: Mon Sep 19, 2005 6:24 am
Location: London
Post
by Jenk » Tue Aug 01, 2006 3:20 am
You're never going to get a completely object based system using PHP (at least with its current incarnation) and will always need at least a small script to initialise the objects. (we can't specify 'public static main ()' like Java/C++)
So your controller would look something like:
Code: Select all
<?php
include '../configs/config.php';
$action = (isset($_GET['action']) ? $_GET['action'] : null);
$front = new Front(new Registry, $action);
?>
Luke
The Ninja Space Mod
Posts: 6424 Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA
Post
by Luke » Tue Aug 01, 2006 9:27 am
good call... this really doesn't need to be a class anyway, huh?
Jenk
DevNet Master
Posts: 3587 Joined: Mon Sep 19, 2005 6:24 am
Location: London
Post
by Jenk » Tue Aug 01, 2006 9:35 am
Entirely your call.
However I'd hazard a guess that 90% of the time, no the front/page controller does not need to be in a class
No part of any application
needs to be OO
Luke
The Ninja Space Mod
Posts: 6424 Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA
Post
by Luke » Tue Aug 01, 2006 11:42 am
so once again... I sort of need to reform my question... I think what I am actually looking for are page controllers...
Here's the new front controller... this works great.
Code: Select all
<?php
class Front{
private
$action,
$dir,
$error;
public function __construct(Registry $registry, $dir='action/', $error='error'){
$this->dir = $dir;
$this->error = $error;
// Get the page request
$request = $registry->get('Request');
// Map page request into readable format
$registry->register('Mapper', null, $request->get('PATH_INFO'));
$mapper = $registry->get('Mapper');
$this->action = $mapper->getAction();
// Load page if it exists
$this->loadAction();
}
private function loadAction(){
require is_file($this->dir . $this->action . '.inc.php') ? $this->dir . $this->action . '.inc.php' : $this->dir . $this->error . '.inc.php';
}
}
?>
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Tue Aug 01, 2006 12:42 pm
What exactly does this do?
Code: Select all
// Map page request into readable format
$registry->register('Mapper', null, $request->get('PATH_INFO'));
$mapper = $registry->get('Mapper');
Shouldn't it be something like:
Code: Select all
// Map page request into readable format
$mapper = $registry->get('Mapper');
$mapper->setPathInfo($request->get('PATH_INFO');
(#10850)
Luke
The Ninja Space Mod
Posts: 6424 Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA
Post
by Luke » Tue Aug 01, 2006 12:53 pm
well my version just sent the path info in the constructor... but that works too. Is there anything wrong with sending it to the constructor?
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Tue Aug 01, 2006 1:05 pm
The Ninja Space Goat wrote: well my version just sent the path info in the constructor... but that works too. Is there anything wrong with sending it to the constructor?
Part of the point of a Registry/Service Locator is that you don't have to know how to construct something. If you want to do it manually then I would be explict about it rather than relying on magic elsewhere.
(#10850)
Luke
The Ninja Space Mod
Posts: 6424 Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA
Post
by Luke » Tue Aug 01, 2006 1:22 pm
good point... thank you.
One question:
Say this is login.inc.php which has been loaded by my front controller
Code: Select all
<?php
class Login{
function __construct(){
}
}
print_r(get_defined_vars());
?>
It only prints variables within the $loadAction() method. I thought there weren't any problems with including files within a class...
alvinphp
Forum Contributor
Posts: 380 Joined: Wed Sep 21, 2005 11:47 am
Post
by alvinphp » Tue Aug 01, 2006 2:35 pm
I wouldn't load a file directly from a class. I would pass the name of the file back to my controller and then let my controller load the page. I never like having my class display/output anything as that is the job of the view/controller. It only passes data back to the controller which then decides where to display the data.
Luke
The Ninja Space Mod
Posts: 6424 Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA
Post
by Luke » Tue Aug 01, 2006 2:39 pm
lol thanks... not sure why I didn't think of that.