## Apache Zeta Components Manual :: File Source for router.php

## Source for file router.php

Documentation is available at router.php

1. <?php
2. /**
3. * File containing the ezcMvcRouter class
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
23. * @version //autogentag//
24. * @filesource
25. * @package MvcTools
26. */
28. /**
29. * The abstract router that you need to inherit from to supply your routes.
30. *
31. * @package MvcTools
32. * @version //autogentag//
33. * @mainclass
34. */
35. abstract class ezcMvcRouter
36. {
37. /**
38. * Contains all the user defined routes.
39. *
40. * @var array(ezcMvcRoute)
41. */
42. protected $routes = array();
44. /**
45. * Contains the request object
46. *
47. * @var ezcMvcRequest
48. */
49. protected $request ;
51. /**
52. * Creates a new router object
53. *
54. * @param ezcMvcRequest $request
55. */
56. public function __construct ( ezcMvcRequest $request )
57. {
58. $this-> request = $request;
59. }
61. /**
62. * User implemented method that should provide all the routes.
63. *
64. * It should return an array of objects that implement the ezcMvcRoute
65. * interface. This could be objects of the ezcMvcRegexpRoute class for
66. * example.
67. *
68. * @return array(ezcMvcRoute)
69. */
70. abstract public function createRoutes ();
72. /**
73. * Returns routing information, including a controller classname from the set of routes.
74. *
75. * This method is run by the dispatcher to obtain a controller. It uses the
76. * user implemented createRoutes() method from the inherited class to fetch the
77. * routes. It then loops over these routes in order - the first one that
78. * matches the request returns the routing information. The loop stops as
79. * soon as a route has matched. In case none of the routes matched
80. * with the request data an exception is thrown.
81. *
82. * @throws ezcMvcNoRoutesException when there are no routes defined.
83. * @throws ezcBaseValueException when one of the returned routes was not
84. * actually an object implementing the ezcMvcRoute interface.
85. * @throws ezcMvcRouteNotFoundException when no routes matched the request URI.
86. * @return ezcMvcRoutingInformation
87. */
88. public function getRoutingInformation ()
89. {
90. $routes = $this-> createRoutes ();
92. if ( ezcBase :: inDevMode () && ( ! is_array ( $routes ) || ! count ( $routes ) ) )
93. {
94. throw new ezcMvcNoRoutesException ();
95. }
97. foreach ( $routes as $route )
98. {
99. if ( ezcBase :: inDevMode () && !$route instanceof ezcMvcRoute )
100. {
101. throw new ezcBaseValueException ( 'route', $route, 'instance of ezcMvcRoute' );
102. }
104. $routingInformation = $route->matches( $this-> request );
105. if ( $routingInformation !== null )
106. {
107. // Add the router to the routing information struct, so that
108. // can be passed to the controllers for reversed route
109. // generation.
110. $routingInformation->router = $this;
112. return $routingInformation;
113. }
114. }
116. throw new ezcMvcRouteNotFoundException ( $this-> request );
117. }
119. /**
120. * Loops over all the given routes and adds the prefix $prefix to them
121. *
122. * The methods loops over all the routes in the $routes variables and calls
123. * the prefix() method on the route with the $prefix. The $prefix should be
124. * a prefix that the route understands.
125. *
126. * @throws ezcMvcRegexpRouteException if the prefix can not be prepended to
127. * one or more of the patterns in the routes.
128. * @param mixed $prefix
129. * @param array(ezcMvcRoute) $routes
130. */
131. static public function prefix ( $prefix, $routes )
132. {
133. foreach ( $routes as $route )
134. {
135. $route->prefix( $prefix );
136. }
137. return $routes;
138. }
140. /**
141. * Generates an URL back out of a route, including possible arguments
142. *
143. * @param mixed $routeName
144. * @param array $arguments
145. */
146. public function generateUrl ( $routeName, array $arguments = null )
147. {
148. $routes = $this->createRoutes();
149. if ( !isset( $routes[$routeName] ) )
150. {
151. throw new ezcMvcNamedRouteNotFoundException( $routeName );
152. }
153. if ( $routes[$routeName] instanceof ezcMvcReversibleRoute )
154. {
155. return $routes[$routeName]-> generateUrl ( $arguments );
156. }
157. else
158. {
159. throw new ezcMvcNamedRouteNotReversableException ( $routeName, get_class ( $routes[$routeName] ) );
160. }
161. }
162. }
163. ?>

Documentation generated by phpDocumentor 1.4.3