簡體   English   中英

Laravel刀片視圖相對路徑

[英]Laravel blade view relative path

我的Laravel Blade視圖需要幫助。 目前,我每次都使用<link href="{{asset('css/sample.css')}}" rel="stylesheet">生成公用文件夾中我sample.css的路徑。 它會生成類似<link href="http://mydomainname/css/sample.css" rel="stylesheet"> ,並且運行良好(我的公共文件夾在我的public_html文件夾cpanel托管上)

但是,我想使用相對路徑而不是直接鏈接到我的css文件。 我想使用一種更簡單的方法,例如<link href="css/sample.css" rel="stylesheet"> ,如果我在根路由上運行,它可以工作,但是當它進入我的網站的深層路由時,則無法工作。

誰能幫助您生成我的CSS文件的相對路徑?

謝謝。

使用您建議的更簡單的方法可以實現這一點,因為您只缺少一個/

如果使用這樣的路徑鏈接到CSS文件; /css/sample.css然后告訴瀏覽器獲取與根URL相關的文檔。 這與您使用的任何資產相同。

因此,作為HTML鏈接應為:

<link href="/css/sample.css" rel="stylesheet">

無論您在什么頁面上,這都將告訴瀏覽器從http(s)://example.com/css/sample.css提取sample.css文件。

開頭沒有/ ,您的鏈接說相對於當前頁面獲取它; http(s)://example.com/your/page/url/css/sample.css 在您的首頁上,這顯然會成功,因為URL仍將解析為http(s)://example.com/css/sample.css

根據此,您只需要編輯位於public文件夾中的index.php ,如下所示:

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

$app = require_once __DIR__.'/../bootstrap/app.php';

// set the public path to this directory
$app->bind('path.public', function() {
    return __DIR__;
});

通過將vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\helpers.php為:

function asset($path, $secure = null)
{
    $url = app('url')->asset($path, $secure);
    return parse_url($url, PHP_URL_PATH);
}

現在,我的laravel中的所有asset()代碼僅生成路徑(通過刪除域URL),並且可以在我的生產環境或本地主機上工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM