未找到 Laravel Blade 布局视图 [layout.default]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21728387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Blade Layout View [layout.default] not found
提问by Asif
I've just started using "Laravel". But the code snippets isn't working.
我刚刚开始使用“Laravel”。但是代码片段不起作用。
inside my DemoController.php
在我的 DemoController.php 中
<?php class DemoController extends BaseController
{
public $restful = true;
public $layout = 'layout.default';
public function get_index()
{
$this->layout->title = 'laravelpage';
$View = View::make('demo1.index' , array(
'name'=>'Laravel user',
'age'=>'28',
'location'=>'dhaka'));
$this->layout->content = $View;
}
}
and inside my index.blade.php
在我的 index.blade.php 里面
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ $title }}</title>
</head>
<body>
{{ $content }}
</body>
</html>
my route is:
我的路线是:
Route::controller('demo1','DemoController');
why it's showing this error?
为什么它显示这个错误?
How can I resolve it?
我该如何解决?
采纳答案by Miroslav Trninic
You mean Blade Layout ?
你是说刀片布局?
First,instead of get_index, use just indexmethod name in you controller. Second, try to use resourceful controllers- mapping from your route to you domain logic will be much more clear, and you'll have whole resource generated. For that purpose use great laravel generator package.
首先,在控制器中只使用index方法名称而不是get_index。其次,尝试使用资源丰富的控制器- 从路由到域逻辑的映射将更加清晰,并且您将生成整个资源。为此,请使用很棒的laravel 生成器包。
Third, I dont see any connection between your blade variables and provided array ?
第三,我没有看到您的刀片变量和提供的数组之间有任何联系?
Using generators is optional, just a good practice.
使用生成器是可选的,只是一个很好的做法。
Better solution is:
更好的解决办法是:
public function index()
{
$my_array ["title"=>"some_title","content"=>"some_content"];
return View::make("my_view")->with("my_array",$my_array);
//or
return View::make("my_view",compact("my_array"));
}
UPDATE:
更新:
Besides other things that can go wrong in you app (permissions,laravel version...), you need to follow very basic pattern in order this to work:
除了您的应用程序中可能出现的其他问题(权限、laravel 版本...)之外,您还需要遵循非常基本的模式才能使其正常工作:
Create a new route with:
使用以下命令创建新路由:
Route::get("demo1","DemoController@index");
Create a new view (index.blade.php file) in your views folder (or subfolder).
在您的视图文件夹(或子文件夹)中创建一个新视图(index.blade.php 文件)。
Create a new controller:
创建一个新的控制器:
class DemoController extends BaseController{
public function index()
{
$my_array = ["title"=>"some_title","content"=>"some_content"];
return View::make("index")->with("my_array",$my_array);
}
}
And in index.blade.php you can put something like:
在 index.blade.php 中,您可以输入以下内容:
{{$title}}
{{$content}}
Now go to browser and you should see title and content, after refresh. I guess your server settings are right.
现在转到浏览器,刷新后您应该会看到标题和内容。我猜你的服务器设置是正确的。
回答by alexrussell
As I read your question, you actually need two views here:
当我阅读您的问题时,您实际上需要两个视图:
- layout/default.blade.php
- demo1/index.blade.php
- 布局/default.blade.php
- 演示1/index.blade.php
That's why it says the view cannot be found - you've created the second of those two views and you're using it in your controller's action, however you don't appear to have created the layout's view that you refer to in the controller.
这就是为什么它说找不到视图的原因 - 您已经创建了这两个视图中的第二个,并且正在控制器的操作中使用它,但是您似乎没有创建您在控制器中引用的布局视图.
Furthermore, you're actually trying to access the layout variables (title
and content
) in your inner view and not your layout. The variables available to each view are as follows:
此外,您实际上是在尝试访问内部视图中的布局变量(title
和content
),而不是您的布局。每个视图可用的变量如下:
- layout.default:
- title (laravelpage)
- content (the HTML of the demo1.index view)
- demo1.index
- name (Laravel user)
- age (28)
- location (dhaka)
- 布局。默认:
- 标题(laravelpage)
- 内容(demo1.index 视图的 HTML)
- 演示1.index
- 名称(Laravel 用户)
- 年龄 (28)
- 地点(达卡)