编程学习网 > PHP技术 > laravel > laravel 辅助函数url()和asset()区别
2019
12-04

laravel 辅助函数url()和asset()区别

laravel 辅助函数url()和asset()区别
就我个人理解,二者基本没什么差别,但看网上说,url()主要用于生成一个完整的网址,asset()一般用于引入静态文件css,js,img等,

我查看源码发现,二者其实就有一个小差别,url()可以传递三个参数,asset只能传递两个参数,源码如下:

function url($path = null, $parameters = [], $secure = null)
    {
        if (is_null($path)) {
            return app(UrlGenerator::class);
        }
 
        return app(UrlGenerator::class)->to($path, $parameters, $secure);
    }
 
function asset($path, $secure = null)
    {
        return app('url')->asset($path, $secure);
    }

举例说明:

url('img/home1.png/'),生成的链接:http://localhost/img/home1.png/

url('img/home1.png/','test'),生成的链接:http://localhost/img/home1.png/test

url('img/home1.png/','test',true),生成的链接:https://localhost/img/home1.png/test

asset('img/home1.png/'),生成的链接:http://localhost/img/home1.png/

asset('img/home1.png/',true),生成的链接:https://localhost/img/home1.png/

扫码二维码 获取免费视频学习资料

Python编程学习

查 看2022高级编程视频教程免费获取