1: <?php
2: /**
3: * WsUrl
4: * Constructs URLs.
5: *
6: * Example usage:
7: *
8: * <code>
9: * // returns '/server_address/site/index/'
10: * // returns '/server_address/index.php?request=site/index'
11: * // servers.
12: * $url = WsUrl::link('site', 'index');
13: *
14: * // returns '/server_address/site/edit_item/id=2/'
15: * // returns '/server_address/index.php?request=site/index&id=2'
16: * $url = WsUrl::link('site', 'index', array('id'=>2));
17: *
18: * // points to file '/server_name/public/js/jquery.min.js'
19: * $asset = WsUrl::asset('js/jquery.min.js');
20: * </code>
21: *
22: */
23: class WsUrl
24: {
25: /**
26: * Constructs URL address.
27: *
28: * If web page is served by Apache HTTP server then uses mod_rewrite and
29: * .htaccess file to construct prety urls. On other web servers returns
30: * standard url string in form '/index.php?request='.
31: *
32: * @param string $controller Name of controller.
33: * @param string $action Name of controller action.
34: * @param array $params Optional parameters
35: * @return string $url
36: *
37: */
38: public static function link($controller, $action, $params = array())
39: {
40: // if we have support for semantic (pretty) urls
41: if (WsConfig::get('pretty_urls') == 'yes'){
42: $url = WsSERVER_ROOT.'/'.$controller.'/'.$action.'/';
43: if (count($params) > 0) {
44: while ($name = current($params)) {
45: $url .= urlencode($name).'/';
46: next($params);
47: }
48: }
49: } else {
50: $url = WsSERVER_ROOT.'/index.php?request='
51: .$controller.'/'.$action.'/';
52: if (count($params) > 0) {
53: while ($name = current($params)) {
54: $url = $url.'&'.urlencode(key($params)).'='.urlencode($name);
55: next($params);
56: }
57: }
58: }
59:
60: return $url;
61: }
62:
63:
64: /**
65: * Construct URLs for static files.
66: *
67: * Static files are placed in /public directory.
68: *
69: * @param string $file File name
70: * @return string $asset
71: *
72: */
73: public static function asset($file)
74: {
75: $fileName = WsROOT.'/public/'.$file;
76: $asset = '';
77:
78: if (file_exists($fileName)) {
79: $asset = WsSERVER_ROOT.'/public/'.$file;
80: }
81:
82: return $asset;
83: }
84: }
85:
86: