所以我有这个view1.php,我想在其中将来自不同链接的不同值传递给view2.php。我设法传递并获取了值,但我在layout.php中放入的CSS不适用于view2.php。我不知道我在哪里做错了。除此以外,还有其他方法可以传递不同的值吗?
view1.php
@extends('layouts.layout')
@section('content')
<div>
<a href="/pass/6000"> OP 6000 </a>
<a href="/pass/10000"> OP 10000 </a>
<a href="/pass/20000"> OP 20000 </a>
</div>
@endsection
web.php
Route::get('/pass/{id}','User\MyController@postID');
MyController.php
public function postID($id) {
return view('user.view2', [
'id' => $id
]);
}
view2.php
@extends('layouts.layout')
@section('content')
<div>
{{$id}}
</div>
@endsection
layout.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>{{ config('app.name', 'Laravel') }}</title>
<link rel="stylesheet" type="text/css" href="css/materialize.css" media="screen,projection"/>
<link rel="stylesheet" href="css/bootstrap/bootstrap.css">
<link rel="stylesheet" href="css/user.css">
<script type="text/javascript" src="js/main.js"></script>
</head>
<body>
<nav> </nav>
<main class="">
@yield('content')
</main>
<footer> </footer>
@yield('alert')
</body>
<script type="text/javascript" src="js/materialize.min.js"></script>
</html>
似乎css没有加载到view2.php中。您需要使用baseurl加载CSS。
示例:<link rel="stylesheet" href="{{URL::asset('css/user.css')}}">
发生此问题的原因是,当您重定向另一个视图时,CSS无法正确加载。
我对您的建议是使用assets
方法。
<link rel="stylesheet" type="text/css" href="{{ asset('css/materialize.css') }}" media="screen,projection"/>
<link rel="stylesheet" href="{{ asset('css/bootstrap/bootstrap.css') }}">
<link rel="stylesheet" href="{{ asset('css/user.css') }}">
仅因为view2的URL为http://something.com/pass/some_id
,并且您要通过css/some_css.css
链接样式表,以便它在当前目录(即pass
)中进行搜索,所以样式表的URL变得像http://something.com/pass/css/some_css.css
。这是个问题。
如果使用/css/some_cs.css
,它将尝试从主目录(即/
目录)中找到文件,因此URL将变为http://something.com/css/some_css.css
,并且一切正常。
否则,您应该使用Laravel提供的帮助程序{{asset('css/some_css.css')}}
它将生成URL,文件将无误地定位在其中。