Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ebf90c8

Browse files
Fix indentation
1 parent 4c78a59 commit ebf90c8

File tree

7 files changed

+336
-336
lines changed

7 files changed

+336
-336
lines changed

‎config/utilities.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
*/
66
function getUri() : string
77
{
8-
$uri = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
9-
$uri = explode('/', $uri);
10-
array_shift($uri);
11-
$uri = implode('/', $uri);
12-
13-
return $uri;
8+
$uri = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
9+
$uri = explode('/', $uri);
10+
array_shift($uri);
11+
$uri = implode('/', $uri);
12+
13+
return $uri;
1414
}
1515

1616
/**
@@ -20,16 +20,16 @@ function getUri() : string
2020
* @return void
2121
*/
2222
function view(string $view, array $data = []) : void
23-
{
24-
$file = APPROOT . '/src/views/' . $view . '.php';
25-
// Check for view file
26-
if (is_readable($file))
27-
{
28-
require_once $file;
29-
}
30-
else
31-
{
32-
// View does not exist
33-
die('<h1> 404 Page not found </h1>');
34-
}
23+
{
24+
$file = APPROOT . '/src/views/' . $view . '.php';
25+
// Check for view file
26+
if (is_readable($file))
27+
{
28+
require_once $file;
29+
}
30+
else
31+
{
32+
// View does not exist
33+
die('<h1> 404 Page not found </h1>');
34+
}
3535
}

‎public/css/style.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
html {
2-
font-family: sans-serif;
2+
font-family: sans-serif;
33
}
44

55
form {
6-
display: inline-block;
6+
display: inline-block;
77
}

‎src/app/Router.php

Lines changed: 144 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -8,148 +8,148 @@
88
*/
99
class Router
1010
{
11-
private $routes = [];
12-
private $params = [];
13-
14-
/**
15-
* Method used to load routes.php file
16-
* @param string $file Path to file
17-
* @return Router Instance of router in order to use the other methods
18-
*/
19-
public static function load(string $file) : Router
20-
{
21-
$router = new static;
22-
23-
require_once $file;
24-
25-
return $router;
26-
}
27-
28-
/**
29-
* Register a route
30-
* Process to convert the route (string) to a regular expression in order to have a more flexible router
31-
* @param string $route Route string, either full route or regular expression
32-
* @param array $params This array stores 'controller' and 'action of each route (if passed)
33-
* @return void
34-
*/
35-
public function addRoute(string $route, array $params = []) : void
36-
{
37-
// Escape forward slashes
38-
$route = preg_replace('/\//', '\\/', $route);
39-
40-
// Convert variables e.g. {controller}
41-
$route = preg_replace('/\{([a-z0-9]+)\}/', '(?P<1円>[a-z0-9-]+)', $route);
42-
43-
// Convert variables with custom regular expressions e.g. {id:\d+} for numbers
44-
$route = preg_replace('/\{([a-z0-9]+):([^\}]+)\}/', '(?P<1円>2円)', $route);
45-
46-
// Add start and end delimeter
47-
$route = '/^' . $route . '$/i';
48-
49-
$this->routes[$route] = $params;
50-
}
51-
52-
/**
53-
* This is where you set the controller and action taken from the URI (like 'posts/add')
54-
* ... and save those into $this->params
55-
* This method is called in routes.php
56-
* @param string $uri Full route like 'task/add-task'
57-
* @return void
58-
*/
59-
public function setParams(string $uri) : void
60-
{
61-
// Store parameters for current 'controller' and 'action'
62-
foreach ($this->routes as $route => $params)
63-
{
64-
if (preg_match($route, $uri, $matches))
65-
{
66-
67-
foreach ($matches as $key => $match)
68-
{
69-
if (is_string($key))
70-
{
71-
if ($key === 'controller')
72-
{
73-
$match = ucwords($match);
74-
}
75-
76-
$params[$key] = $match;
77-
}
78-
}
79-
80-
$this->params = $params;
81-
}
82-
}
83-
}
84-
85-
/**
86-
* Call requested controller->action()
87-
* At this point $this->params is = ['controller' => 'MyController', 'action' => 'desiredMethod', 'id' => 1]
88-
* So first check if such 'controller' exists and 'action' is a callable method
89-
* If so, then unset 'controller' and 'action', which leaves $this->params = ['id' => 1]
90-
* And last, the desired function is called with all the parameters (array) like 'id'...
91-
* ... and this can be used or not if the method doesn't receive any arguments
92-
* @return void
93-
*/
94-
public function redirect() : void
95-
{
96-
$controller = $this->getNamespace() . $this->params['controller'];
97-
$action = $this->capitalizeAction($this->params['action']);
98-
99-
if (class_exists($controller))
100-
{
101-
$controller = new $controller;
102-
unset($this->params['controller']);
103-
104-
if (is_callable([$controller, $action]))
105-
{
106-
unset($this->params['action']);
107-
unset($this->params['namespace']);
108-
}
109-
else
110-
{
111-
die('Page not found.');
112-
}
113-
}
114-
else
115-
{
116-
header('location: ' . URLROOT);
117-
}
118-
119-
call_user_func_array([$controller, $action], [$this->params]);
120-
}
121-
122-
/**
123-
* Get the namespace for the requested controller class
124-
* @return string $namespace String for the complete namespace
125-
*/
126-
private function getNamespace() : string
127-
{
128-
$namespace = '\\Controllers\\';
129-
130-
if (array_key_exists('namespace', $this->params))
131-
{
132-
$namespace .= $this->params['namespace'] . '\\';
133-
}
134-
135-
return $namespace;
136-
}
137-
138-
/**
139-
* Camel case the 'action' string
140-
* If in the URL the action is something like '{controller}/add-new-task'...
141-
* ... change it to 'addNewTask' in order to make it match with a class method
142-
* @param string $action Get the 'action' from the URL
143-
* @return string Camel Cased 'action' string
144-
*/
145-
private function capitalizeAction(string $action) : string
146-
{
147-
$action = explode('-', $action);
148-
149-
for($i=1; $i < count($action); $i++){
150-
$action[$i] = ucwords($action[$i]);
151-
}
152-
153-
return implode($action);
154-
}
11+
private $routes = [];
12+
private $params = [];
13+
14+
/**
15+
* Method used to load routes.php file
16+
* @param string $file Path to file
17+
* @return Router Instance of router in order to use the other methods
18+
*/
19+
public static function load(string $file) : Router
20+
{
21+
$router = new static;
22+
23+
require_once $file;
24+
25+
return $router;
26+
}
27+
28+
/**
29+
* Register a route
30+
* Process to convert the route (string) to a regular expression in order to have a more flexible router
31+
* @param string $route Route string, either full route or regular expression
32+
* @param array $params This array stores 'controller' and 'action of each route (if passed)
33+
* @return void
34+
*/
35+
public function addRoute(string $route, array $params = []) : void
36+
{
37+
// Escape forward slashes
38+
$route = preg_replace('/\//', '\\/', $route);
39+
40+
// Convert variables e.g. {controller}
41+
$route = preg_replace('/\{([a-z0-9]+)\}/', '(?P<1円>[a-z0-9-]+)', $route);
42+
43+
// Convert variables with custom regular expressions e.g. {id:\d+} for numbers
44+
$route = preg_replace('/\{([a-z0-9]+):([^\}]+)\}/', '(?P<1円>2円)', $route);
45+
46+
// Add start and end delimeter
47+
$route = '/^' . $route . '$/i';
48+
49+
$this->routes[$route] = $params;
50+
}
51+
52+
/**
53+
* This is where you set the controller and action taken from the URI (like 'posts/add')
54+
* ... and save those into $this->params
55+
* This method is called in routes.php
56+
* @param string $uri Full route like 'task/add-task'
57+
* @return void
58+
*/
59+
public function setParams(string $uri) : void
60+
{
61+
// Store parameters for current 'controller' and 'action'
62+
foreach ($this->routes as $route => $params)
63+
{
64+
if (preg_match($route, $uri, $matches))
65+
{
66+
67+
foreach ($matches as $key => $match)
68+
{
69+
if (is_string($key))
70+
{
71+
if ($key === 'controller')
72+
{
73+
$match = ucwords($match);
74+
}
75+
76+
$params[$key] = $match;
77+
}
78+
}
79+
80+
$this->params = $params;
81+
}
82+
}
83+
}
84+
85+
/**
86+
* Call requested controller->action()
87+
* At this point $this->params is = ['controller' => 'MyController', 'action' => 'desiredMethod', 'id' => 1]
88+
* So first check if such 'controller' exists and 'action' is a callable method
89+
* If so, then unset 'controller' and 'action', which leaves $this->params = ['id' => 1]
90+
* And last, the desired function is called with all the parameters (array) like 'id'...
91+
* ... and this can be used or not if the method doesn't receive any arguments
92+
* @return void
93+
*/
94+
public function redirect() : void
95+
{
96+
$controller = $this->getNamespace() . $this->params['controller'];
97+
$action = $this->capitalizeAction($this->params['action']);
98+
99+
if (class_exists($controller))
100+
{
101+
$controller = new $controller;
102+
unset($this->params['controller']);
103+
104+
if (is_callable([$controller, $action]))
105+
{
106+
unset($this->params['action']);
107+
unset($this->params['namespace']);
108+
}
109+
else
110+
{
111+
die('Page not found.');
112+
}
113+
}
114+
else
115+
{
116+
header('location: ' . URLROOT);
117+
}
118+
119+
call_user_func_array([$controller, $action], [$this->params]);
120+
}
121+
122+
/**
123+
* Get the namespace for the requested controller class
124+
* @return string $namespace String for the complete namespace
125+
*/
126+
private function getNamespace() : string
127+
{
128+
$namespace = '\\Controllers\\';
129+
130+
if (array_key_exists('namespace', $this->params))
131+
{
132+
$namespace .= $this->params['namespace'] . '\\';
133+
}
134+
135+
return $namespace;
136+
}
137+
138+
/**
139+
* Camel case the 'action' string
140+
* If in the URL the action is something like '{controller}/add-new-task'...
141+
* ... change it to 'addNewTask' in order to make it match with a class method
142+
* @param string $action Get the 'action' from the URL
143+
* @return string Camel Cased 'action' string
144+
*/
145+
private function capitalizeAction(string $action) : string
146+
{
147+
$action = explode('-', $action);
148+
149+
for($i=1; $i < count($action); $i++){
150+
$action[$i] = ucwords($action[$i]);
151+
}
152+
153+
return implode($action);
154+
}
155155
}

‎src/controllers/Index.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
class Index
55
{
6-
public function home()
7-
{
8-
view('Index/home');
9-
}
6+
public function home()
7+
{
8+
view('Index/home');
9+
}
1010

11-
public function about()
12-
{
13-
view('Index/about');
14-
}
11+
public function about()
12+
{
13+
view('Index/about');
14+
}
1515
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /