繁体   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