-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouting.html
116 lines (110 loc) · 4.88 KB
/
routing.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!DOCTYPE html>
<html lang="en">
<head>
<title>Menukaarten-docs</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="assets/css/stylesheet.css" media="screen,print">
<link rel="stylesheet" href="assets/css/print.css" media="print">
<link rel="stylesheet" type="text/css" href="assets/css/shCore.css" media="screen,print">
<link rel="stylesheet" type="text/css" href="assets/css/shThemeDefault.css" media="screen,print">
<script type="text/javascript" src="assets/js/jquery.min.js"></script>
<script type="text/javascript" src="assets/js/SyntaxHighlighter.js"></script>
<script type="text/javascript" src="assets/js/build_menu.js"></script>
</head>
<body>
<div id="header-wrapper">
<div id="header">
<h1>Documentation SexyFramework</h1>
<span>Created by Vincent Bremer & Douwe de Haan</span>
</div>
</div>
<div id="container">
<div id="menu-wrapper">
<div id="menu">
<h1>Table of contents</h1>
<ul></ul>
</div>
</div>
<div id="content-wrapper">
<div id="content">
<!-- START CONTENT -->
<h1>Router</h1>
<p>The router makes sure that the request that comes in will call the right controller and the right method. It also makes it possible to have change the controller that will be called via custom routes.</p>
<h2>Default behaviour</h2>
<p>The router gets the requested URL from the .htaccess file, which stores it in the $_GET['req'] variable. The router then breaks it down into an array, in which the first value is the controller, the second one the method and the third one an id. This is shown in the following example.<p>
<pre class="brush: plain">
http://www.website.com/<controller>/<action>/<id>
</pre>
<p>If there are more values than the three that were mentioned, the rest of the values is stored in in the same order as the URL. Those values can be retrieved by using the function get($part), where $part is the number at which place the value is stored in the array. This function is explained further in this section</p>
<p>If noting comes after the base URL, the router will load the default controller and method, which are specified in ???</p>
<h2>Retrieving URL parts</h2>
<p>If for some reason it is needed to get a part of the URL, the function get() could be used. This function explodes the URL into pieces. For example we use the URL 'www.example.com/users/view/432'. The get() function returns each part as follows:</p>
<table>
<thead>
<tr>
<th>Function with parameter</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr>
<td>get('url')</td>
<td>www.example.com/users/view/432</td>
</tr>
<tr>
<td>get('request')</td>
<td>/users/view/432</td>
</tr>
<tr>
<td>get('page')</td>
<td>users</td>
</tr>
<tr>
<td>get(0)</td>
<td>users</td>
</tr>
<tr>
<td>get('controller')</td>
<td>users_controller</td>
</tr>
<tr>
<td>get('action')</td>
<td>view</td>
</tr>
<tr>
<td>get('1')</td>
<td>view</td>
</tr>
<tr>
<td>get('id')</td>
<td>432</td>
</tr>
<tr>
<td>get(2)</td>
<td>432</td>
</tr>
</tbody>
</table>
<h2>Subfolders</h2>
<p>It is also possible to put controllers in a subfolder in the controller folder. Before any controller is included, the router checks wether there is a folder that has the same name as the first part of the requested URL. If this is true, it continues the same way as it would normally do, but then in the subfolder.</p>
<h2 id="custom_routes">Custom Routes</h2>
<p>For some reasons, like SEO, there is a way to change the appearance of the URL. In the configuration folder, there is a file called routes.json, which looks like the following code:</p>
<pre class="brush: js">
{
"about": "staticpages:about",
"contact": "staticpages:contact",
"login": "cms/users:login",
"logout": "cms/users:logout",
"profile/:id": "users:view"
}
</pre>
<p>The column at the left is the URL that was requested. The router will always check this file first, to see if there is a route that matches the requested URL. If there is a match, the router will take the right column and explode the string. The part in front of the colon is the controller that has to be searched for. The part after the colon the method in that controller. </p>
<p>Note that the third and fourth entry have a controller with a slash in it. The router will check for slashes and explode the string. The part in front of the slash indicates that the controller file is placed in a subfolder. The part after the slash is the controller name.</p>
<p>The last feature that custom routes has, allows you to get some extra parameters passed from the URL. These variables can be accessed in the right controller by the function get('id'), where the name of the parameter is the same as you defined in the routes.</p>
<!-- END CONTENT -->
</div>
</div>
</div>
<script type="text/javascript" src="assets/js/SyntaxHighlighter_settings.js"></script>
</body>
</html>