我是 PHP 和 Laravel 的新手。我正在制作一个 Laravel 登录应用程序,它在比较输入值和数据库值后打印“成功”。
我的问题是:当两个值都匹配时,它会打印“成功”,但当它们不匹配时,它会显示 ModelNotFoundException。
我为此创建了一个异常(exception),但同样的错误再次发生。
提前致谢!
这是我的 route.php 代码
route.php
Route::get('register', 'RegisterController@register');
Route::post('register/show', 'RegisterController@show');
Route::post('register/store', 'RegisterController@store');
Route::get('register/login', 'RegisterController@login');
这是我的注册 Controller ,它从 index.blade.php 和名为 registers 的数据库中获取值,该数据库有两列 username 和 < strong>密码
RegisterController.php
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Register;
use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Request;
class RegisterController extends Controller {
public function register()
{
return view('register.index');
}
public function store()
{
$input = Request::all();
Register::create($input);
return redirect('register');
}
public function show()
{
try
{
$result2 = Request::get('username');
$result = Register::where('username', $result2)->firstOrFail();
return view('register.show', compact('result','result2'));
}
catch(ModelNotFoundException $e)
{
return "login fail" . redirect('register/login');
}
}
public function login()
{
return view('register.login');
}
}
注册.php
use Illuminate\Database\Eloquent\Model;
class Register extends Model {
protected $fillable = ['username', 'password'];
}
创建注册表
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRegistersTable extends Migration {
public function up()
{
Schema::create('registers', function(Blueprint $table)
{
$table->increments('id');
$table->string('username');
$table->string('password');
$table->timestamps();
});
}
public function down()
{
Schema::drop('registers');
}
}
index.blade.php
@extends('master')
@section('login')
{!! Form::open(['url' => 'register/store']) !!}
{!!Form::label('username','Name:')!!}
{!!Form::text('username', null)!!}
<br>
{!!Form::label('password','Password:')!!}
{!!Form::text('password', null)!!}
{!!Form::submit('submit')!!}
{!! Form::close() !!}
@stop
login.blade.php
@extends('master')
@section('register')
{!! Form::open(['url' => 'register/show']) !!}
{!!Form::label('username','Name:')!!}
{!!Form::text('username', null)!!}
<br>
{!!Form::label('password','Password:')!!}
{!!Form::text('password', null)!!}
{!!Form::submit('submit')!!}
{!! Form::close() !!}
@stop
** show.blade.php **
@extends('master')
@section('show')
<?php
if( $result->username == $result2 )
{
echo "success";
}
?>
@stop
最佳答案
您是否已将错误捕获逻辑添加到您的 app/Exceptions/Handler.php 文件中?
查看 http://laravel.com/docs/5.0/eloquent ;特别是它指出的地方:
通过主键检索模型或抛出异常
有时您可能希望在未找到模型时抛出异常。为此,您可以使用 firstOrFail 方法:
$model = User::findOrFail(1);
$model = User::where('votes', '>', 100)->firstOrFail();
这样做可以让您捕获异常,以便您可以根据需要记录并显示错误页面。要捕获 ModelNotFoundException,向您的 app/Exceptions/Handler.php 文件添加一些逻辑。
use Illuminate\Database\Eloquent\ModelNotFoundException;
class Handler extends ExceptionHandler {
public function render($request, Exception $e)
{
if ($e instanceof ModelNotFoundException)
{
// Custom logic for model not found...
}
return parent::render($request, $e);
}
}
关于php - 在 Laravel 5 中创建登录应用程序并出现 ModelNotFoundException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31604723/