-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working router functionality. Needs testing
- Loading branch information
1 parent
a362e22
commit 2b3e030
Showing
6 changed files
with
182 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/** | ||
* Created by IntelliJ IDEA. | ||
* User: Aleksander Akerø | ||
* Date: 27.03.2016 | ||
* Time: 05.41 | ||
*/ | ||
class APP_Router { | ||
private $path; | ||
private $defaultController; | ||
private $defaultAction; | ||
|
||
private $controller; | ||
private $action; | ||
|
||
public function __construct($path) { | ||
$this->log = Logger::getLogger("com.dalisra.request"); | ||
|
||
$this->path = $path; | ||
$this->defaultController = APP::$conf['routing']['defaultController']; | ||
$this->defaultAction = APP::$conf['routing']['defaultAction']; | ||
} | ||
|
||
public function parse() { | ||
if($this->path == null) | ||
$this->path = ''; | ||
|
||
// TODO: Possible bug with this removing the GET parameters ...? | ||
$chunks = explode('/', $this->path); | ||
|
||
if(strlen($chunks[0]) < 1) { | ||
$this->controller = $this->defaultController; | ||
$this->action = $this->defaultAction; | ||
|
||
} else { | ||
$controllerName = array_shift($chunks); | ||
$controllerFile = APP::$conf['path']['controllers'] . $controllerName . '.class.php'; | ||
$actionName = array_shift($chunks); | ||
|
||
/** Assuming error until proven wrong ...Moahahah! */ | ||
$this->controller = 'App_Controller'; | ||
$this->action = '404'; | ||
|
||
if(file_exists($controllerFile)) { | ||
$this->controller = $controllerName; | ||
|
||
if(strlen($actionName) > 0 ) $this->action = $actionName; | ||
else $this->action = $this->defaultAction; | ||
|
||
} else { | ||
$urlmapper = new App_UrlMapper(); | ||
$route = $urlmapper->getMappedRoute($this->path); | ||
|
||
if (count($route) > 0) { | ||
$controllerFile = APP::$conf['path']['controllers'] . $route['controller'] . '.class.php'; | ||
|
||
if (file_exists($controllerFile)) { | ||
$this->controller = $route['controller']; | ||
$this->action = $route['action']; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
public function getController() | ||
{ | ||
return $this->controller; | ||
} | ||
|
||
public function getAction() | ||
{ | ||
return $this->action; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
/** | ||
* Created by IntelliJ IDEA. | ||
* User: Aleksander Akerø | ||
* Date: 27.03.2016 | ||
* Time: 05.43 | ||
*/ | ||
class App_UrlMapper { | ||
var $routes = array( | ||
array( | ||
'url' => '/^login/', | ||
'controller' => 'authorize', | ||
'action' => 'login' | ||
), | ||
array( | ||
'url' => '/^logout/', | ||
'controller' => 'authorize', | ||
'action' => 'logout' | ||
) | ||
); | ||
|
||
public function getMappedRoute($url) { | ||
$mappedRoute = []; | ||
|
||
foreach($this->routes as $route) { | ||
if(preg_match($route['url'], $url)) { | ||
$mappedRoute = $route; | ||
} | ||
} | ||
|
||
return $mappedRoute; | ||
} | ||
} |
2b3e030
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This starting to get into shape but few things:
2b3e030
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 and 2. I agree that we don't necessarily need the request class. That was just the way I made it while testing the implementation on an a smaller scale, and it was just easier to move the entire class in to this project this way. But I see what, you mean, we could basicly just move the functions in to the Request class.
But what I think works with it is that the logic is simplified now. The processRequest function only does the logic of what happens given a request, and the router helps with giving it the correct parameters as of what the controller, action model and so on is.
Also the authorize controller is not a part of the core framework so maybe we should find some more common or relatable routes to put in as default. And yes, the routes could maybe come from a config file, or database or whatever. The UrlMapper was just a proof of consept.
And url-mapper is maybe the wrong name for this class. Maybe that is the one that should be called Router, since it does not provide any url's for linking and so on, it just a route for a given request.
2b3e030
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prototype kode: