8000 Merge pull request #111 from jackmcdade/5.0 · Hydrane/tmp-laravel-framework@21f673b · GitHub
[go: up one dir, main page]

Skip to content

Commit 21f673b

Browse files
committed
Merge pull request laravel#111 from jackmcdade/5.0
Add in asset and secureAsset methods and their dependent class methods
2 parents 19d4a92 + b6f6fcb commit 21f673b

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

src/Routing/UrlGenerator.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,88 @@ public function to($path, $extra = array(), $secure = null)
9191
return $this->trimUrl($root, $path, $tail);
9292
}
9393

94+
/**
95+
* Generate a secure, absolute URL to the given path.
96+
*
97+
* @param string $path
98+
* @param array $parameters
99+
* @return string
100+
*/
101+
public function secure($path, $parameters = array())
102+
{
103+
return $this->to($path, $parameters, true);
104+
}
105+
106+
/**
107+
* Generate a URL to an application asset.
108+
*
109+
* @param string $path
110+
* @param bool|null $secure
111+
* @return string
112+
*/
113+
public function asset($path, $secure = null)
114+
{
115+
if ($this->isValidUrl($path)) return $path;
116+
117+
// Once we get the root URL, we will check to see if it contains an index.php
118+
// file in the paths. If it does, we will remove it since it is not needed
119+
// for asset paths, but only for routes to endpoints in the application.
120+
$root = $this->getRootUrl($this->getScheme($secure));
121+
122+
return $this->removeIndex($root).'/'.trim($path, '/');
123+
}
124+
125+
/**
126+
* Remove the index.php file from a path.
127+
*
128+
* @param string $root
129+
* @return string
130+
*/
131+
protected function removeIndex($root)
132+
{
133+
$i = 'index.php';
134+
135+
return str_contains($root, $i) ? str_replace('/'.$i, '', $root) : $root;
136+
}
137+
138+
/**
139+
* Generate a URL to a secure asset.
140+
*
141+
* @param string $path
142+
* @return string
143+
*/
144+
public function secureAsset($path)
145+
{
146+
return $this->asset($path, true);
147+
}
148+
149+
/**
150+
* Get the scheme for a raw URL.
151+
*
152+
* @param bool|null $secure
153+
* @return string
154+
*/
155+
protected function getScheme($secure)
156+
{
157+
if (is_null($secure))
158+
{
159+
return $this->forceSchema ?: $this->request->getScheme().'://';
160+
}
161+
162+
return $secure ? 'https://' : 'http://';
163+
}
164+
165+
/**
166+
* Force the schema for URLs.
167+
*
168+
* @param string $schema
169+
* @return void
170+
*/
171+
public function forceSchema($schema)
172+
{
173+
$this->forceSchema = $schema.'://';
174+
}
175+
94176
/**
95177
* Get the URL to a named route.
96178
*

0 commit comments

Comments
 (0)
0