问题:将我的Laravel 4代码转换为Laravel 5.2。我正在移动和转换我的观点,并且无法通过以下错误。
错误: IndexController.php第27行中的ErrorException: 尝试分配非对象的属性
调试信息:
请求请协助弄清楚错误出现的原因,如果可能的话,请详细说明如何防止错误以及我做错了什么。
注意:我曾经为种子数据库播种后在Laravel 4上收到类似的错误,但我能够刷新迁移并重新设置数据库,一切都会重新开始。这在Laravel 5中不适用于此错误。此代码适用于L4。
尝试:我在Google上阅读了很多内容并尝试了各种项目,例如 php artisan clear-compiled , composer dump-autoload , php artisan optimize 无济于事。我相信错误来自 $ numberofpcs = new additionalPCs();但是我无法证实这一点。我还删除了我发送到视图的所有变量,但错误仍然存在,因此看起来像 $ this-> layout-> content = View :: make('index');
IndexController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Library\additionalPCs;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use View;
use App\Models\businesstype;
use App\Models\contractterm;
class IndexController extends BaseController
{
Protected $layout = 'master';
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
/** Wayne - 03-02-2014 - Moved for loop to a method within its own class. */
$numberofpcs = new additionalPCs();
$addtpcs=$numberofpcs->display();
$this->layout->content = View::make('index')->with('addtpcs', $addtpcs)->with('businesstypelist', businesstype::dropdown())->with('contracttermlist',ContractTerm::dropdown());
}
}
additionalPCs.php
<?php
namespace App\Library;
use App\Library\additionalComputer;
class additionalPCs extends additionalComputer {
public function display() {
return $this->displayMenu();
}
}
additionalComputer.php
<?php
namespace App\Library;
/** Counts up the Number of Additional PC Options
* and stores them into an array then sends them to the view.
*/
class additionalComputer {
protected function displayMenu() {
$addtpcs= [];
for ($i=0; $i <= 100; $i++) {
$addtpcs[$i] = $i;
}
return $addtpcs;
}
}
BaseController.php - (这只是为了表明我有一个IndexController的BaseController.php文件。我知道L5没有附带一个默认设置。)
<?php
namespace App\Http\Controllers;
class BaseController extends Controller {
/**
* Setup the layout used by the controller.
*
* @return void
*/
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$this->layout = View::make($this->layout);
}
}
}
答案 0 :(得分:0)
Laravel 5中删除了控制器布局功能。因此,setupLayout()
方法未被调用,这意味着您的layout
属性仅保留字符串'master'。由于它只是一个字符串,当你执行$this->layout->content = ...
时,你试图分配一个非对象的属性。
我相信如果你愿意,你应该能够重新加入。在BaseController
中,您需要将callAction
方法覆盖为以前的方式:
public function callAction($method, $parameters)
{
$this->setupLayout();
$response = call_user_func_array(array($this, $method), $parameters);
// If no response is returned from the controller action and a layout is being
// used we will assume we want to just return the layout view as any nested
// views were probably bound on this view during this controller actions.
if (is_null($response) && ! is_null($this->layout)) {
$response = $this->layout;
}
return $response;
}
答案 1 :(得分:0)
Patricus协助我找到了这个答案,但我想发布这个,因为我尝试的答案仍然存在问题。但他是正确的,因为我无法再利用控制器布局。
我改变了
$this->layout->content = View::make('index')->with('addtpcs', $addtpcs)->with('businesstypelist', businesstype::dropdown())->with('contracttermlist',ContractTerm::dropdown());
到
return view('index')->with('addtpcs', $addtpcs)->with('businesstypelist', businesstype::dropdown())->with('contracttermlist',ContractTerm::dropdown());
这解决了这个问题。