简体   繁体   中英

Laravel Blade Layout View [layout.default] not found

I've just started using "Laravel". But the code snippets isn't working.

inside my 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

<!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?

As I read your question, you actually need two views here:

  1. layout/default.blade.php
  2. demo1/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:

  • layout.default:
    • title (laravelpage)
    • content (the HTML of the demo1.index view)
  • demo1.index
    • name (Laravel user)
    • age (28)
    • location (dhaka)

You mean Blade Layout ?

First,instead of get_index , use just index method 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.

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:

Create a new route with:

Route::get("demo1","DemoController@index");

Create a new view (index.blade.php file) in your views folder (or subfolder).

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:

{{$title}}
{{$content}}

Now go to browser and you should see title and content, after refresh. I guess your server settings are right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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