【发布时间】:2014-05-28 09:39:50
【问题描述】:
简单的问题,但似乎很难得到答案。在 Codeigniter 中,我可以加载 URL 帮助程序,然后简单地做
echo base_url();
获取我网站的 URL。 Laravel 中是否有等价物?
【问题讨论】:
简单的问题,但似乎很难得到答案。在 Codeigniter 中,我可以加载 URL 帮助程序,然后简单地做
echo base_url();
获取我网站的 URL。 Laravel 中是否有等价物?
【问题讨论】:
您可以使用 URL 外观来调用 URL generator
所以你可以这样做:
URL::to('/');
你也可以使用应用容器:
$app->make('url')->to('/');
$app['url']->to('/');
App::make('url')->to('/');
或者注入 UrlGenerator:
<?php
namespace Vendor\Your\Class\Namespace;
use Illuminate\Routing\UrlGenerator;
class Classname
{
protected $url;
public function __construct(UrlGenerator $url)
{
$this->url = $url;
}
public function methodName()
{
$this->url->to('/');
}
}
【讨论】:
/something/ 中,它会生成正确的 URL。
{{URL::to('/my-page.html')}} 在视图中,echo URL::to('/my-page.html'); 在代码中
<a class="button is-primary" href="<?= URL::to('/'); ?>/atencion/reporte" target="_blank">,谢谢!
Laravel
echo url();
Laravel >= 5.2
echo url('/');
【讨论】:
url('/')
asset('/') 对我来说似乎更好,因为它有斜杠,这在基本 href 中是必需的。
对于 Laravel 5,我通常使用:
<a href="{{ url('/path/uri') }}">Link Text</a>
我了解到使用url() 函数调用的Facade 与URL::to() 相同
【讨论】:
secure_url() 函数,这将产生一个 https 链接。在 https 站点上使用 url() 仍会生成 http 链接。
url() 会根据请求的协议生成 http 或 https 链接。另一方面,secure_url() 不存在。 Laravel 5.4 也有这种变化吗?
secure_url() 的 5.4 Laravel 文档可在此处获得:laravel.com/docs/5.4/helpers#method-secure-url
2018 Laravel release(5.7) 文档的更新,增加了一些 url() 函数及其用法。
问题:在 Laravel 中获取网站的 URL?
这是一个一般性的问题,所以我们可以拆分它。
1.访问基本 URL
// Get the base URL.
echo url('');
// Get the app URL from configuration which we set in .env file.
echo config('app.url');
2。访问当前 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();
3.命名路由的 URL
// http://example.com/home
echo route('home');
4.资产的 URL(公共)
// Get the URL to the assets, mostly the base url itself.
echo asset('');
5.文件网址
use Illuminate\Support\Facades\Storage;
$url = Storage::url('file.jpg'); // stored in /storage/app/public
echo url($url);
也可以通过 URL 外观访问这些方法中的每一个:
use Illuminate\Support\Facades\URL;
echo URL::to(''); // Base URL
echo URL::current(); // Current URL
如何使用刀片模板(视图)调用这些 Helper 函数。
// http://example.com/login
{{ url('/login') }}
// http://example.com/css/app.css
{{ asset('css/app.css') }}
// http://example.com/login
{{ route('login') }}
// usage
<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
<!-- Login link -->
<a class="nav-link" href="{{ route('login') }}">Login</a>
<!-- Login Post URL -->
<form method="POST" action="{{ url('/login') }}">
【讨论】:
要让它与不漂亮的 URL 一起工作,我必须这样做:
asset('/');
【讨论】:
<base href="{{ asset('/') }}" />
<base href="..."/>,因为斜杠
这个:
echo url('/');
还有这个:
echo asset('/');
在我的例子中都显示了主页 url :)
【讨论】:
Laravel 提供了一堆辅助函数,你可以简单地满足你的要求
使用 Laravel Helpers 的 url() 函数
但在 Laravel 5.2 的情况下,您必须使用 url('/')
here 是 Laravel 的所有其他辅助函数的列表
【讨论】:
url('/css/style.css')
要获取您配置的应用程序网址,您可以使用:
Config::get('app.url')
【讨论】:
app.url 它似乎是一个后备
另一种可能:{{ URL::route('index') }}
【讨论】:
检查这个 -
<a href="{{url('/abc/xyz')}}">Go</a>
这对我有用,我希望它对你有用。
【讨论】:
有多种方式:
request()->getSchemeAndHttpHost()
url('/')
asset('')
$_SERVER['SERVER_NAME']
【讨论】:
你也可以使用 URL::to('/') 在 Laravel 中显示图片。请看下面:
<img src="{{URL::to('/')}}/images/{{ $post->image }}" height="100" weight="100">
假设您的图片存储在“public/images”下。
【讨论】:
我使用了这个,它在 Laravel 5.3.18 中对我有用:
<?php echo URL::to('resources/assets/css/yourcssfile.css') ?>
重要提示:这仅在您已从 URL 中删除“公共”时才有效。为此,您可以查看this helpful tutorial。
【讨论】:
顺便说一句,如果您的路线名称如下:
Route::match(['get', 'post'], 'specialWay/edit', 'SpecialwayController@edit')->name('admin.spway.edit');
您可以像这样使用route() 函数:
<form method="post" action="{{route('admin.spway.edit')}}" class="form form-horizontal" id="form-spway-edit">
其他有用的功能:
$uri = $request->path();
$url = $request->url();
$url = $request->fullUrl();
asset()
app_path();
// ...
https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/helpers.php
【讨论】:
您可以按照以下方式使用外观或辅助功能。
echo URL::to('/');
echo url();
Laravel 使用 Symfony 组件进行请求,Laravel 内部逻辑按照 关注。
namespace Symfony\Component\HttpFoundation;
/**
* {@inheritdoc}
*/
protected function prepareBaseUrl()
{
$baseUrl = $this->server->get('SCRIPT_NAME');
if (false === strpos($this->server->get('REQUEST_URI'), $baseUrl)) {
// assume mod_rewrite
return rtrim(dirname($baseUrl), '/\\');
}
return $baseUrl;
}
【讨论】:
我找到了另一种方法来获取基本 url 以显示环境变量 APP_URL 的值
env('APP_URL')
这将显示基本网址,如http://domains_your//yours_website。请注意,它假定您已在 .env 文件(位于根文件夹中)中设置了环境变量。
【讨论】:
你可以从请求中得到它,在 laravel 5
request()->getSchemeAndHttpHost();
【讨论】:
我还使用了base_path() 函数,它对我有用。
【讨论】: