1.简介
Laravel提供了一些帮助程序来帮助您为应用程序生成URL。
当在模板和API响应中构建链接,或生成指向应用程序另一部分的重定向响应时,这些方法尤其有用。
2.基础
2.1 生成基础URL
url帮助程序可用于为您的应用程序生成任意URL。 生成的URL将自动使用该方案(HTTP或HTTPS)并从当前请求中托管:
$post = App\Models\Post::find(1);
echo url("/posts/{$post->id}");
// http://example.com/posts/1
2.2 访问当前URL
如果没有提供URL帮助器的路径,则返回Illuminate \ Routing \ UrlGenerator实例,使您可以访问有关当前URL的信息:
// Get the current URL without the query string...
echo url()->current();
// Get the current URL including the query string...
echo url()->full();
// Get the full URL for the previous request...
echo url()->previous();
还可以通过URL外观访问以下每种方法:
use Illuminate\Support\Facades\URL;
echo URL::cu