在 Laravel 视图中使用 CSS?

2022-01-16 00:00:00 frameworks php laravel

我刚开始学习 Laravel,可以做控制器和路由的基础知识.

I've just began learning Laravel, and can do the basics of a controller and routing.

我的操作系统是 Mac OS X Lion,它位于 MAMP 服务器上.

My OS is Mac OS X Lion, and it's on a MAMP server.

我的来自 routes.php 的代码:

My code from routes.php:

Route::get('/', function() {
    return View::make('home.index');
});

Route::get('businesses', function() {
    return View::make('businesses.index');
});

Route::get('testing', function() {
    return View::make('testing.index');
});        

Route::get('hello', function() {
    return "<h3>Hello world!</H3>";
});

这行得通,视图显示完美,但是"我想要尝试在视图中包含 CSS,我尝试在目录中添加指向样式表的链接,但页面将其显示为默认值浏览器字体,即使 css 在 HTML 中!

That works, the views display perfectly, ''however'' what I want to try and do is include CSS within the views, I tried adding in a link to a stylesheet within the directory but the page displayed it as the default browser font even though the css was in the HTML!

这是视图文件夹中企业的 index.php:

This is index.php from businesses within the views folder:

<head>
   <link rel="stylesheet" type="text/css" href="mystyle.css">
</head>

<p>Business is a varied term. My content here.

我尝试在我的其他视图文件夹(测试)中使用 Blade 模板引擎来显示 CSS,但是尽管 CSS 在测试文件夹中,但仍然没有显示 CSS!

I tried using the Blade template engine in my other views folder (testing) to display CSS but again the CSS did not show despite it being in the testing folder!

我怎样才能克服这个问题,变得更好 - 因为我正在慢慢学习这个框架.

How can I overcome this problem, and get better - as I'm slowly learning this framework.

推荐答案

把你的资产放到公用文件夹中;例如:

Put your assets in the public folder; e.g.:

public/css
public/images
public/fonts
public/js

然后,要使用 Laravel 访问它们,请使用:

And then, to access them using Laravel, use:

{{ HTML::script('js/scrollTo.js'); }}

{{ HTML::style('css/css.css'); }}

或者:

{{ URL::asset('js/scrollTo.js'); }}

{{ URL::asset('css/css.css'); }} 

此语法将自动生成正确的路径(例如,`public/js/scrollTo.js').

This syntax will automatically generate the correct path (e.g., `public/js/scrollTo.js').

相关文章