Skip to content

Commit

Permalink
Learning MVC part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
atornblad committed Apr 27, 2015
1 parent d4e7377 commit 0f40652
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Routing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

class Routing {
public $controllerClassName;
public $methodName;
public $parameter;

/**
* Processes a URL for MVC routing
*
* Parses a URL into a controller part, a view part, and a parameter part.
* All parts are optional. The controller and view parts have the default
* values of Home and Index.
*
* @param string $url The path part of a URL - after the initial /
* @return object
*/
public function handle($url) {
$parts = explode('/', $url);

$controllerName = @$parts[0];
$methodName = @$parts[1];
$parameter = @$parts[2];

if (!$controllerName) $controllerName = 'Home';
if (!$methodName) $methodName = 'Index';

return (object) [
'controllerClassName' => $controllerName . 'Controller',
'methodName' => $methodName,
'parameter' => $parameter
];
}
}

?>
69 changes: 69 additions & 0 deletions RoutingTests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

class RoutingTests {
/**
* @test
*/
public function CheckAllDefaults() {
// Arrange
$routing = new Routing();

// Act
$route = $routing->handle('');

// Assert
The($route->controllerClassName)->shouldEqual('HomeController');
The($route->methodName)->shouldEqual('Index');
The($route->parameter)->shouldNotBeSet();
}

/**
* @test
*/
public function CheckDefaultMethodNameAndParameter() {
// Arrange
$routing = new Routing();

// Act
$route = $routing->handle('Articles');

// Assert
The($route->controllerClassName)->shouldEqual('ArticlesController');
The($route->methodName)->shouldEqual('Index');
The($route->parameter)->shouldNotBeSet();
}

/**
* @test
*/
public function CheckDefaultParameter() {
// Arrange
$routing = new Routing();

// Act
$route = $routing->handle('Categories/List');

// Assert
The($route->controllerClassName)->shouldEqual('CategoriesController');
The($route->methodName)->shouldEqual('List');
The($route->parameter)->shouldNotBeSet();
}

/**
* @test
*/
public function CheckNoDefaults() {
// Arrange
$routing = new Routing();

// Act
$route = $routing->handle('Products/Item/123x');

// Assert
The($route->controllerClassName)->shouldEqual('ProductsController');
The($route->methodName)->shouldEqual('Item');
The($route->parameter)->shouldEqual('123x');
}
}

?>
7 changes: 7 additions & 0 deletions mvc.komodoproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Komodo Project File - DO NOT EDIT -->
<project id="6859751f-864b-4e0e-9c36-697664ad1b5e" kpf_version="5" name="mvc.komodoproject">
<preference-set idref="6859751f-864b-4e0e-9c36-697664ad1b5e" id="project" preftype="project">
<long id="prefs_version">1</long>
</preference-set>
</project>

0 comments on commit 0f40652

Please sign in to comment.