]> BookStack Code Mirror - bookstack/blob - app/UrlGenerator.php
471792cdf18b7251b377d6b1d6d3e68ab165896e
[bookstack] / app / UrlGenerator.php
1 <?php
2
3 namespace BookStack;
4
5 class UrlGenerator extends \Illuminate\Routing\UrlGenerator
6 {
7
8     /**
9      * Generate an absolute URL to the given path.
10      *
11      * @param  string  $path
12      * @param  mixed  $extra
13      * @param  bool|null  $secure
14      * @return string
15      */
16     public function to($path, $extra = [], $secure = null)
17     {
18         $tail = implode('/', array_map(
19                 'rawurlencode', (array) $this->formatParameters($extra))
20         );
21
22         $defaultRoot = $this->formatRoot($this->formatScheme($secure));
23
24         list($path, $query) = $this->extractQueryString($path);
25
26         return $this->formatWithBase(
27                 $defaultRoot, trim($path.'/'.$tail, '/')
28             ).$query;
29     }
30
31     /**
32      * Format the given URL segments into a single URL.
33      *
34      * @param  string  $defaultRoot
35      * @param  string  $path
36      * @return string
37      */
38     public function formatWithBase($defaultRoot, $path)
39     {
40         $isFullPath = strpos($path, 'http') === 0;
41         $setBasePath = trim(config('app.url'), '/');
42
43         if ($isFullPath) {
44             return $path;
45         }
46
47         if (! empty($setBasePath)) {
48             $defaultRoot = $setBasePath;
49         }
50
51         // TODO - Add mechanism to set path correctly for intended() and back() redirects
52         // TODO - Update tests to align with new system
53         // TODO - Clean up helpers and refactor their usage.
54
55         return trim($defaultRoot. '/' .$path, '/');
56     }
57
58 }