简体   繁体   English

.css文件中的图像链接,或在laravel中使用.js文件调用控制器

[英]image link inside .css file OR call a controller using .js file in laravel

I am using Laravel 4 我正在使用Laravel 4

How can I add a link to a image inside a .css or .js file. 如何在.css或.js文件中添加指向图像的链接。

If I put a relative path like that: 如果我这样说相对路径:

background-image: url("../assets/images/assets.png");

it will load the file only if my view is like that 仅当我的视图是这样时,它才会加载文件

/mysite/users

if the url of my view change from 如果我的观点的网址从

/mysite/users 

to

/mysite/users/job/10/something

It will not load. 它不会加载。

I can put the full path like that: 我可以这样写完整的路径:

    background-image: url("/mysite/assets/images/assets.png");

but if I have to create a copy of this site like 'theSite', I will have to open All my css files and .js file and replace /mysite to /theSite 但是,如果我必须创建该站点的副本(例如“ theSite”),则必须打开我的所有CSS文件和.js文件,并将/ mysite替换为/ theSite

Are there a smarty way to do that? 有什么聪明的方法吗? like I use in the view: 就像我在视图中使用的那样:

{{ URL::to('style.css') }}

.

EDITED

The ceejayoz is correct. ceejayoz是正确的。

But if you need a .js file that call a Controller you could do this: 但是,如果您需要一个调用Controller的.js文件,则可以执行以下操作:

create this route: 创建此路线:

Route::get('/js/custom.js ', function()
{

    $contents = View::make('js/custom');
    $response = Response::make($contents, 200);
    $response->header('Content-Type', 'application/javascript');
    return $response;
});

create the custom.blade.php view and put this js content: 创建custom.blade.php视图并将此js内容放入:

$('#inputX').change(function() {
    $.ajax({                                      
            url: "{{URL::to('track/data')}}",
            type: "GET",
            cache: false,
            async: sync,                         
            data: {
                doit: $('#inputX').val()
            },
            success: function(data)          
            {       
                console.log(data)
            }
    });     

});

What do you think about it? 你怎么看待这件事?

URLs in CSS files are relative to the CSS file, not the page it's used on. CSS文件中的URL是相对于CSS文件的,而不是相对于其使用的页面的。 It should not matter what the URL of the page you're on is - if your CSS is in public/css and you use ../images/something.jpg , it'll use public/images/something.jpg regardless of the URL of the page. 所访问页面的URL是什么都无关紧要-如果CSS位于public/css并且您使用../images/something.jpg ,则无论使用哪种方式,它都将使用public/images/something.jpg 。页面的URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM