在Laravel 5中创建登录应用程序并获取ModelNotFoundException错误

时间:2015-07-24 07:35:09

标签: php mysql laravel-5

我是PHP和Laravel的新手。我正在制作一个Laravel登录应用程序,在比较输入的值和数据库值后打印“成功”。

我的问题是:当两个值都匹配时,它会打印“成功”但当它们不匹配时会显示 ModelNotFoundException

我为此创建了一个例外,但同样发生了同样的错误。

提前谢谢!

这是我的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');

这是我的注册控制器,它从 index.blade.php 和名为寄存器的数据库中获取值,该数据库有两列用户名密码

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');
    }
}

Register.php

    use Illuminate\Database\Eloquent\Model;

    class Register extends Model {

        protected $fillable = ['username', 'password'];


    }

CreateRegistersTable

  <?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

1 个答案:

答案 0 :(得分:2)

您是否已将错误捕获逻辑添加到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);
  }

}