2014-11-05 58 views
0

我正在使用控制器來調用在我的某個視圖中定義的窗體。在控制器中,我有一個簡單的'create()'方法,我只是想在使用我定義的'Video'模型時調用表單。問題是當我嘗試調用這個函數時,我不斷收到'從空值創建默認對象'錯誤。Laravel PHP:從空值錯誤中創建默認對象

控制器(應用\ VideosController.php):

<?php 

use Acme\repositories\VideoRepository; 

use Models\Video; 

use Models\Validators as Validators; 

class VideosController extends BaseController { 

/** 
* The video model 
* 
* @var \Models\Video 
**/ 
protected $video; 


/* The value defined from my repository 
*/ 

protected $videoRepo; 

/** 
* Instantiate the controller 
* 
* @param Models\Video $video 
* @return void 
**/ 

    public function __construct(VideoRepository $videoRepo) 
    { 
    $this->videoRepo = $videoRepo; 
    /* Old code below 
    $this->video = \App::make('Repositories\VideoRepository'); 
    */ 
    } 

/* Create a new video by passing in a sub view 
Of the form for creating a new video */ 

    public function create() 
    { 
    $data = array('type' => 'video'); 
    $this->layout->content = \View::make('forms.new-video', $data); 
    } 

} 

模型(APP /模型/ video.php):

<?php 


class Video extends Eloquent { 

    /** 
    * The table used by this model 
    * 
    * @var string 
    **/ 
    protected $table = 'videos'; 

    /** 
    * The primary key 
    * 
    * @var string 
    **/ 
    protected $primaryKey = 'video_id'; 

    /** 
    * The fields that are guarded cannot be mass assigned 
    * 
    * @var array 
    **/ 
    protected $guarded = array(); 

    /** 
    * Enabling soft deleting 
    * 
    * @var boolean 
    **/ 
    protected $softDelete = true; 


    } 

用於我的形式的視圖位於'app \ views \ forms \ new-video.blade.php'。

難道我沒有正確定義我的'create()'方法,或者是不把文件放在正確的位置嗎?

回答

1

我相信你缺少你的控制器$layout屬性的聲明,所以它不知道作爲模板使用什麼看法:

/** 
* The layout that should be used for responses. 
*/ 
protected $layout = 'layouts.master'; 

http://laravel.com/docs/4.2/templates#controller-layouts

+0

這樣做的竅門!謝謝! – 2014-11-05 21:51:12

相關問題