当我转到(例如)/project/2
时,我试图返回一个视图,但我得到了以下错误:
未定义的变量:项目(视图:../resources/views/project.blade.php)
请记住,我对开发和Laravel
还很陌生。我正在使用Laravel 5.2
。
这是我的控制器:
<?php
namespace App'Http'Controllers;
use Illuminate'Http'Request;
use App'Project;
use App'Http'Controllers'Controller;
class pagesController extends Controller {
public function index() {
$projects = 'App'Project::orderBy('created_at', 'desc')->get();
return view('index')->with('projects', $projects);
}
public function storeProject(Request $request) {
$project = new Project;
$project->name = $request->newProject;
$project->save();
}
public function getProject($id) {
$project = 'App'Project::findorfail($id); //findorfail 404 teruggeven wanneer id null is.
return view('project')->with('project', $project);
}
}
我的表单:
<!-- Begin nieuw project formulier-->
<div class="dropdown-container">
<div class="text-center dropdownwrap">
<form role="form" method="POST" action="/project/">
<div class="form-group">
<input name="newProject" type="text" class="form-control" id="newProjectField" placeholder="Voeg project toe..."/>
</div>
<div class="form-group text-center">
<button type="submit" class="btn btn-primary">
Bewaar
</button>
</div>
</form>
</div>
<a href="#" class="btn btn-default btn-block dropdownButton"><span class="glyphicon glyphicon-plus"></span></a>
</div>
<!-- Einde Nieuw project formulier-->
我的路线:
<?php
/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', 'pagesController@index');
Route::get('/test', function() {
return view('testPage');
}
);
Route::post('/', 'pagesController@storeProject');
Route::post('/project', 'pagesController@storeProject');
Route::get('/project/{id}', 'pagesController@getProject');
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/
Route::group(['middleware' => ['web']], function () {
//
});
根据您的路由URI project/2
触发以下方法:
public function getProject($id)
{
$project = 'App'Project::findorfail($id); //findorfail 404 teruggeven wanneer id null is.
return view('project')->with('project', $project);
}
在这种情况下,您使用了project
而不是projects
,因此变量$projects
将不可用,而是使用$project
。更清楚地说,当您编写)->with('project', $project);
时,您的意思是,with
方法中的第一个参数将是访问第二个参数中传递的数据的变量名,在您的情况下,您使用了with('project', $project)
。所以它的工作原理是这样的:
return view('project')->with(
'project', // <-- This will be available in the view as $project
$project // <-- This will be contained in the $project variable in the view
);