我一直在做一个' laravel'项目,现在想要实现身份验证。我做过:制作:auth'并按预期在项目中显示文件夹,路径,视图等。
我已添加登录链接并在我的主页顶部注册。当选择这些链接中的任何一个时,他们会尝试按照您的预期重定向...因此,单击“登录”会将URL更改为/ login,但是,我没有看到登录页面内容。我收到以下错误消息:
尝试获取非对象的属性
它突出显示了我的评论页面上的<?php echo e($film->title); ?>
。
我不介意用户是否能够在未登录时查看所有页面和内容。我只是添加了auth,以便我可以为用户存储评论。
为什么它会给我这个错误/我该如何解决?
路线:
/* display list of all films*/
Route::get('/', 'FilmsController@display');
/* display film comments*/
Route::get('/{id}', 'FilmsController@comment');
Route::post('/{id}', 'FilmsController@addComment')->name('addComment');
Route::get('/delete/{id}', 'FilmsController@deleteComment')->name('deleteComment');
Route::get('/update/{id}', 'FilmsController@editComment')->name('editComment');
Route::post('/update/{id}', 'FilmsController@saveComment')->name('saveComment');
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
主页视图:
<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Films</title>
</head>
<body>
<div class="flex-center position-ref full-height">
@if (Route::has('login'))
<div class="top-right links">
@auth
<a href="{{ url('/home') }}">Home</a>
@else
<a href="{{ route('login') }}">Login</a>
<a href="{{ route('register') }}">Register</a>
@endauth
</div>
@endif
<h1>Films</h1>
<table id="myTable">
<tr>
<th>Title</th>
<th>Director</th>
<th>Description</th>
<th>Released</th>
</tr>
@foreach ($films as $film)
<tr>
<td>
<a href="/{{ $film->id }}">{{$film->title}}</a>
</td>
<td>{{$film->director}}</td>
<td>{{$film->description}}</td>
<td>{{ Carbon\Carbon::parse($film->date)->format('l\\, jS \\of F Y\\, h:i A') }}</td>
</tr>
@endforeach
</body>
</html>
评论视图
<h1>{{ $film->title }}</h1>
<!--<p>{{$film->comments}}</p>-->
<table id="myTable">
<tr>
<th>Comment</th>
<th>User</th>
<th>Update</th>
<th>Delete</th>
@foreach ($film->comments as $comment)
<tr>
<td>{{$comment->body}}</td>
<td>Lorem</td>
<td><a href="/update/{{$comment->id}}">Update</a></td>
<td><a href="/delete/{{$comment->id}}">Delete</a></td>
</tr>
@endforeach
</table>
<div>@include('form')</div>
家庭控制器和视图仍然是由make:auth
...
主页视图
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Dashboard</div>
<div class="panel-body">
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
You are logged in!
</div>
</div>
</div>
</div>
</div>
@endsection
家庭控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('home');
}
}
电影控制器功能,与显示评论有关:
/* Displays comments for a film*/
public function comment($id)
{
$film = Film::find($id);
return view('comment', compact('film'));
}
我的所有其他代码都与此不同。当我点击登录/注册链接时,我收到此错误。我根本看不到页面内容。它会直接指向对象错误。
答案 0 :(得分:1)
错误是由此行引起的:
$film
您需要从呈现Film
视图的控制器方法返回comments
中包含return view('comments', ['film' => Film::find(1)]);
对象的web.php
。例如:
Route::get('/{id}', 'FilmsController@comment');
Route::post('/{id}', 'FilmsController@addComment')->name('addComment');
另外,将这些路由放在S&P 500
文件的末尾:
(http://portfolios.morningstar.com/fund/index-summary?t=SPX®ion=usa&culture=en-US)