[php] 라 라벨 : 기본 URL 받기

간단한 질문이지만 답을 찾기가 매우 어려워 보입니다. Codeigniter에서 URL 도우미를로드 한 다음 간단히

echo base_url();

내 사이트의 URL을 가져옵니다. Laravel에 해당하는 것이 있습니까?



답변

URL 생성기를 호출 할 수있는 URL 파사드를 사용할 수 있습니다.

그래서 당신은 할 수 있습니다 :

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('/');
    }
}


답변

라 라벨 <5.2

echo url();

라 라벨> = 5.2

echo url('/');

희망이 당신을 도울


답변

Laravel 5의 경우 일반적으로 다음을 사용합니다.

<a href="{{ url('/path/uri') }}">Link Text</a>

url()함수 를 사용 하는 것과 같은 것을 호출 한다는 것을 이해하고 Facade있습니다.URL::to()


답변

더 많은 url () 함수를 사용하여 2018 Laravel 릴리스 (5.7) 설명서에서 업데이트되었으며 사용법입니다.

질문 : 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. 파일 URL

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

사용법과 함께 블레이드 템플릿 (보기)에서 이러한 도우미 함수를 호출하는 방법

// 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('/');


답변

라 라벨은 다양한 헬퍼 기능을 제공하며 요구 사항에 따라 간단하게

라 라벨 헬퍼의 url () 함수 사용

그러나 Laravel 5.2의 경우 url ( ‘/’) 을 사용해야합니다

여기 Laravel의 다른 모든 도우미 기능의 목록입니다


답변

이:

echo url('/');

이:

echo asset('/');

둘 다 내 경우에는 홈 URL을 표시했습니다. 🙂