Development

/branches/1.2/lib/controller/sfWebController.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/controller/sfWebController.class.php

Revision 17858, 5.0 kB (checked in by FabianLange, 5 years ago)

[1.1, 1.2, 1.3] fixed some codeing standards (fixes #6376 - patch from gimler)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  * (c) 2004-2006 Sean Kerr <sean@code-box.org>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * sfWebController provides web specific methods to sfController such as, url redirection.
14  *
15  * @package    symfony
16  * @subpackage controller
17  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
18  * @author     Sean Kerr <sean@code-box.org>
19  * @version    SVN: $Id$
20  */
21 abstract class sfWebController extends sfController
22 {
23   /**
24    * Generates an URL from an array of parameters.
25    *
26    * @param mixed   $parameters An associative array of URL parameters or an internal URI as a string.
27    * @param boolean $absolute   Whether to generate an absolute URL
28    *
29    * @return string A URL to a symfony resource
30    */
31   public function genUrl($parameters = array(), $absolute = false)
32   {
33     // absolute URL or symfony URL?
34     if (is_string($parameters) && preg_match('#^[a-z][a-z0-9\+.\-]*\://#i', $parameters))
35     {
36       return $parameters;
37     }
38
39     // relative URL?
40     if (is_string($parameters) && 0 === strpos($parameters, '/'))
41     {
42       return $parameters;
43     }
44
45     if (is_string($parameters) && $parameters == '#')
46     {
47       return $parameters;
48     }
49
50     $route = '';
51     $fragment = '';
52
53     if (is_string($parameters))
54     {
55       // strip fragment
56       if (false !== ($pos = strpos($parameters, '#')))
57       {
58         $fragment = substr($parameters, $pos + 1);
59         $parameters = substr($parameters, 0, $pos);
60       }
61
62       list($route, $parameters) = $this->convertUrlStringToParameters($parameters);
63     }
64     else if (is_array($parameters))
65     {
66       if (isset($parameters['sf_route']))
67       {
68         $route = $parameters['sf_route'];
69         unset($parameters['sf_route']);
70       }
71     }
72
73     // routing to generate path
74     $url = $this->context->getRouting()->generate($route, $parameters, $absolute);
75
76     if ($fragment)
77     {
78       $url .= '#'.$fragment;
79     }
80
81     return $url;
82   }
83
84   /**
85    * Converts an internal URI string to an array of parameters.
86    *
87    * @param string $url An internal URI
88    *
89    * @return array An array of parameters
90    */
91   public function convertUrlStringToParameters($url)
92   {
93     $givenUrl = $url;
94
95     $params = array();
96     $queryString = '';
97     $route = '';
98
99     // empty url?
100     if (!$url)
101     {
102       $url = '/';
103     }
104
105     // we get the query string out of the url
106     if ($pos = strpos($url, '?'))
107     {
108       $queryString = substr($url, $pos + 1);
109       $url = substr($url, 0, $pos);
110     }
111
112     // 2 url forms
113     // @routeName?key1=value1&key2=value2...
114     // module/action?key1=value1&key2=value2...
115
116     // first slash optional
117     if ($url[0] == '/')
118     {
119       $url = substr($url, 1);
120     }
121
122
123     // routeName?
124     if ($url[0] == '@')
125     {
126       $route = substr($url, 1);
127     }
128     else if (false !== strpos($url, '/'))
129     {
130       list($params['module'], $params['action']) = explode('/', $url);
131     }
132     else if (!$queryString)
133     {
134       $route = $givenUrl;
135     }
136     else
137     {
138       throw new InvalidArgumentException(sprintf('An internal URI must contain a module and an action (module/action) ("%s" given).', $givenUrl));
139     }
140
141     // split the query string
142     if ($queryString)
143     {
144       $matched = preg_match_all('/
145         ([^&=]+)            # key
146         =                   # =
147         (.*?)               # value
148         (?:
149           (?=&[^&=]+=) | $  # followed by another key= or the end of the string
150         )
151       /x', $queryString, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
152       foreach ($matches as $match)
153       {
154         $params[urldecode($match[1][0])] = urldecode($match[2][0]);
155       }
156
157       // check that all string is matched
158       if (!$matched)
159       {
160         throw new sfParseException(sprintf('Unable to parse query string "%s".', $queryString));
161       }
162     }
163
164     return array($route, $params);
165   }
166
167   /**
168    * Redirects the request to another URL.
169    *
170    * @param string $url        An existing URL
171    * @param int    $delay      A delay in seconds before redirecting. This is only needed on
172    *                           browsers that do not support HTTP headers
173    * @param int    $statusCode The status code
174    */
175   public function redirect($url, $delay = 0, $statusCode = 302)
176   {
177     $url = $this->genUrl($url, true);
178
179     if (sfConfig::get('sf_logging_enabled'))
180     {
181       $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Redirect to "%s"', $url))));
182     }
183
184     // redirect
185     $response = $this->context->getResponse();
186     $response->clearHttpHeaders();
187     $response->setStatusCode($statusCode);
188     $response->setHttpHeader('Location', $url);
189     $response->setContent(sprintf('<html><head><meta http-equiv="refresh" content="%d;url=%s"/></head></html>', $delay, htmlspecialchars($url, ENT_QUOTES, sfConfig::get('sf_charset'))));
190     $response->send();
191   }
192 }
193
Note: See TracBrowser for help on using the browser.