-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes_decicions.html
176 lines (157 loc) · 5.37 KB
/
routes_decicions.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!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>Routes decisions</h1>
<p>The router is responsible for handling the requests to the right controllers, or 404 messages when the users requests a non existing page. Many frameworks use the following format for mapping URL's to controllers:</p>
<pre class="brush: php">
// Format:
website.com/:controller/:action/:id
// Example #1:
website.com/users/edit/592
| | |
controller | |
action |
id
</pre>
<p>Ruby on Rails uses a slightly different approach with handling request:</p>
<pre class="brush: php">
// Format #1:
website.com/:controller/:action
// Format #2
website.com/:controller/:id/:action
// Example #1:
website.com/users/592/edit
| | |
controller | |
id |
action
// Example #2:
website.com/users/new
| |
controller |
action
</pre>
<h2>Routes file</h2>
<p>For creating nice looking URL's it's important to make sure the router first checks a special routes file if the requested URL is already mapped to a certain controller/action. This routes file could be a PHP file, a INI file, a JSON file, or anything else. There isn't really a standard for this, almost all frameworks use different approaches to this.</p>
<p>What actually IS important, is the content of that routes file. All frameworks use different approaches for building a routes file. We've listed a few of the bigger frameworks and check how we could map the following URL's:</p>
<table>
<tr>
<th>Route:</th>
<th>Controller:</th>
<th>Action:</th>
<th>Parameters</th>
</tr>
<tr>
<td>/login</td>
<td>users</td>
<td>login</td>
<td></td>
</tr>
<tr>
<td>/forgot_password</td>
<td>users</td>
<td>forgot_password</td>
<td></td>
</tr>
<tr>
<td>/profile/32</td>
<td>users</td>
<td>view</td>
<td>32</td>
</tr>
<tr>
<td>/users/edit/32</td>
<td>users</td>
<td>edit</td>
<td>32</td>
</tr>
</table>
Routes file: <span class="bold">Ruby on Rails:</span> <sup><a href="#source_1">[1]</a></sup><br>
<pre class="brush: php">
match '/login', to: 'users#login'
match '/forgot_password', to: 'users#forgot_password'
match '/profile/:id', to: 'users#view'
match '/users/edit/:id' to: 'users#edit'
</pre>
Routes file: <span class="bold">CakePHP:</span> <sup><a href="#source_2">[2]</a></sup><br>
<pre class="brush: php">
Router::connect(
'/login',
array('controller' => 'users', 'action' => 'login')
);
Router::connect(
'/forgot_password',
array('controller' => 'users', 'action' => 'forgot_password')
);
Router::connect(
'/profile/*',
array('controller' => 'users', 'action' => 'view')
);
Router::connect(
'/users/edit/*',
array('controller' => 'users', 'action' => 'edit')
);
</pre>
Routes file: <span class="bold">Kohana:</span> <sup><a href="#source_3">[3]</a></sup><br>
<pre class="brush: php">
Route::set('login', '<action>',
array( 'action' => '(login|forgot_password)'
))->defaults(array('controller' => 'users'));
Route::set('profile', 'profile/<id>',
array( 'id' => '\d+')
))->defaults(array('controller' => 'users', 'action' => 'view'));
Route::set('profile', 'users/edit/<id>',
array( 'id' => '\d+')
))->defaults(array('controller' => 'users', 'action' => 'edit'));
</pre>
<h2>Our preferred solution</h2>
Routes file: <span class="bold">routes.json</span>
<pre class="brush: php">
{
'/login': 'users:login',
'/forgot_password': 'users:forgot_password',
'/profile/:id': 'users:view',
'/users/edit/:id': 'users:edit'
}
</pre>
<h3>Why?</h3>
<p>We have chosen for this solution because the functions of CakePHP and Kohana are pretty hard to understand, and for us almost impossible to code. Our solutions is based on the one used in Ruby on Rails, because it's just a few lines long and easy to understand. </p>
<p class="footnote">
[1] - <a href="http://guides.rubyonrails.org/routing.html" id="source_1">Ruby on Rails Guide</a><br>
[2] - <a href="http://book.cakephp.org/2.0/en/development/routing.html#connecting-routes" id="source_2">CakePHP Cookbook</a><br>
[3] - <a href="http://kohanaframework.org/3.0/guide/kohana/routing" id="source_3">Kohana User Guide</a>
</p>
<!-- END CONTENT -->
</div>
</div>
</div>
<script type="text/javascript" src="assets/js/SyntaxHighlighter_settings.js"></script>
</body>
</html>