拉拉维尔 4.2 - 哨兵 - 未返回正确的视图


Laravel 4.2 - Sentry - Not returning the correct view

我正在开发一个带有Laravel 4.2的CRUD应用程序,并使用Sentry 2进行用户注册和分组。我为动物创建了一个模型和一个动物控制器,以便管理员用户可以从数据库中查看/编辑和删除动物。我已经创建了所有视图,例如:views/animales/create.blade.php。但是,当我尝试调用 animales/create view 以显示表单以输入新动物时,我收到一个错误异常:

"试图获取非对象的属性(视图:.../app/views/animales/show.blade.php)"

这实际上不是我所说的观点。以下是我的路线:

Route::group(['before' => 'auth|admin'], function()
{
 Route::resource('animales', 'AnimalController');
}

这是我的动物控制器:

 class AnimalController extends 'BaseController {
            /**
             * Display a listing of the resource.
             *
             * @return Response
             */
            public function index()
            {
                // get all the animals & ganaderias
                $animals = DB::table('animals')->where('categoria', 'Listado')->get();
                $ganaderias = Ganaderia::lists('nombre', 'id'); 

                // load the view and pass the animals
                return View::make('animales.index', array('animals' => $animals, 'ganaderias' => $ganaderias ));
            }

            /**
             * Show the form for creating a new resource.
             *
             * @return Response
             */
            public function create()
            {
                //get all the ganaderias nombres & ids into an array to bind to form select 
                $ganaderias = Ganaderia::lists('nombre', 'id');
                // load the create form (app/views/animales/create.blade.php)
                return View::make('animales.create', array('ganaderias' => $ganaderias));
            }

            /**
             * Store a newly created resource in storage.
             *
             * @return Response
             */
            public function store()
            {
                // store new animal in db
                $input = Input::all();
                Animal::create([
                    'sexo' => $input['sexo'],
                    'registro' => $input['registro'],
                    'dib' => $input['dib'],
                    'fecha_de_nacimiento' => $input['fecha_de_nacimiento'],
                    'fecha_de_calificacion' => $input['fecha_de_calificacion'],
                    'calificacion' => $input['calificacion'],
                    'padre' => $input['padre'],
                    'madre' => $input['madre'],
                    'adn' => $input['adn'],
                    'fecha_de_transaccion' => $input['fecha_de_transaccion'],
                    'observaciones' => $input['observaciones'],
                    'fecha_de_baja' => $input['fecha_de_baja'],
                    'causa_de_baja' => $input['causa_de_baja'],
                    'citogenetica' => $input['citogenetica'],
                    'ganaderia_id' => $input['ganaderia_id'],
                    'categoria' => $input['categoria']

                    ]);
           return Redirect::to('animales');
            }

            /**
             * Display the specified resource.
             *
             * @param  int  $id
             * @return Response
             */
        public function show($id)
                {
                    $animal = Animal::find($id);         
                    $ganaderia = Ganaderia::where( 'id', '=', 'ganaderia_id')->get();
                    // show the view and pass the nerd to it
                    return View::make('animales.show', array('animal' => $animal, 'ganaderia' => $ganaderia ));
                }

            /**
             * Show the form for editing the specified resource.
             *
             * @param  int  $id
             * @return Response
             */
            public function edit($id)
            {
                // get the animal
                $animal = Animal::find($id);
                //get all the ganaderias nombres & ids into an array to bind to form select 
                        $ganaderias = Ganaderia::lists('nombre', 'id');
                // show the view and pass the nerd to it
                return View::make('animales.edit', array('animal' => $animal, 'ganaderia' => $ganaderias ));
            }

            /**
             * Update the specified resource in storage.
             *
             * @param  int  $id
             * @return Response
             */
            public function update($id)
            {
                // validate
                // read more on validation at http://laravel.com/docs/validation
                $rules = array(
                    'sexo'       => 'required',
                    'fecha_de_calificacion'      => 'required',
                    'calificacion' => 'required',
                    'dib' => 'required',
                    'registro' => 'required',
                    'padre' => 'required',
                    'madre' => 'required',
                );
                $validator = Validator::make(Input::all(), $rules);
                // process the login
                if ($validator->fails()) {
                    return Redirect::to('animales/' . $id . '/edit')
                        ->withErrors($validator)
                        ->withInput();
                } else {
                    // store
                    $animal = Animal::find($id);
                    $animal->sexo       = Input::get('sexo');
                    $animal->registro       = Input::get('registro');
                    $animal->dib      = Input::get('dib');
                    $animal->fecha_de_nacimiento = Input::get('fecha_de_nacimiento');
                    $animal->fecha_de_muerte       = Input::get('fecha_de_muerte');
                    $animal->fecha_de_calificacion       = Input::get('fecha_de_calificacion');
                    $animal->calificacion       = Input::get('calificacion');
                    $animal->fecha_de_baja       = Input::get('fecha_de_baja');
                    $animal->causa_de_baja       = Input::get('causa_de_baja');
                    $animal->fecha_de_transaccion       = Input::get('fecha_de_transaccion');
                    $animal->padre       = Input::get('padre');
                    $animal->madre       = Input::get('madre');
                    $animal->observaciones       = Input::get('observaciones');
                    $animal->citogenetica       = Input::get('citogenetica');
                    $animal->adn       = Input::get('adn');
                    $animal->partos       = Input::get('partos');
                    $animal->partos_no_lg       = Input::get('partos_no_lg');
                    $animal->ganaderia_id       = Input::get('ganaderia_id');
                    $animal->categoria          = Input::get('categoria');
                    $animal->save();
                    // redirect
                    Session::flash('message', 'Animal editado correctamente!');
                    return Redirect::to('animales');
                }
            }

            /**
             * Remove the specified resource from storage.
             *
             * @param  int  $id
             * @return Response
             */
            public function destroy($id)
            {
                // delete
                $animal = Animal::find($id);
                $animal->delete();
                // redirect
                Session::flash('message', 'Este animal fue eliminado correctamente!');
                return Redirect::to('animales');
            }

        }

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function edit($id)
{
    // get the animal
    $animal = Animal::find($id);
    //get all the ganaderias nombres & ids into an array to bind to form select 
            $ganaderias = Ganaderia::lists('nombre', 'id');
    // show the view and pass the nerd to it
    return View::make('animales.edit', compact('animal', 'ganaderias'));
}

/**
 * Update the specified resource in storage.
 *
 * @param  int  $id
 * @return Response
 */
public function update($id)
{
    // validate
    // read more on validation at http://laravel.com/docs/validation
    $rules = array(
        'sexo'       => 'required',
        'fecha_de_calificacion'      => 'required',
        'calificacion' => 'required',
        'dib' => 'required',
        'registro' => 'required',
        'padre' => 'required',
        'madre' => 'required',
    );
    $validator = Validator::make(Input::all(), $rules);
    // process the login
    if ($validator->fails()) {
        return Redirect::to('animales/' . $id . '/edit')
            ->withErrors($validator)
            ->withInput();
    } else {
        // store
        $animal = Animal::find($id);
        $animal->sexo       = Input::get('sexo');
        $animal->registro       = Input::get('registro');
        $animal->dib      = Input::get('dib');
        $animal->fecha_de_nacimiento = Input::get('fecha_de_nacimiento');
        $animal->fecha_de_muerte       = Input::get('fecha_de_muerte');
        $animal->fecha_de_calificacion       = Input::get('fecha_de_calificacion');
        $animal->calificacion       = Input::get('calificacion');
        $animal->fecha_de_baja       = Input::get('fecha_de_baja');
        $animal->causa_de_baja       = Input::get('causa_de_baja');
        $animal->fecha_de_transaccion       = Input::get('fecha_de_transaccion');
        $animal->padre       = Input::get('padre');
        $animal->madre       = Input::get('madre');
        $animal->observaciones       = Input::get('observaciones');
        $animal->citogenetica       = Input::get('citogenetica');
        $animal->adn       = Input::get('adn');
        $animal->partos       = Input::get('partos');
        $animal->partos_no_lg       = Input::get('partos_no_lg');
        $animal->ganaderia_id       = Input::get('ganaderia_id');
        $animal->categoria          = Input::get('categoria');
        $animal->save();
        // redirect
        Session::flash('message', 'Animal editado correctamente!');
        return Redirect::to('animales');
    }
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return Response
 */
public function destroy($id)
{
    // delete
    $animal = Animal::find($id);
    $animal->delete();
    // redirect
    Session::flash('message', 'Este animal fue eliminado correctamente!');
    return Redirect::to('animales');
}

}

这是过滤器文件:

                    /*
                    |--------------------------------------------------------------------------
                    | Application & Route Filters
                    |--------------------------------------------------------------------------
                    |
                    | Below you will find the "before" and "after" events for the application
                    | which may be used to do any work before or after a request into your
                    | application. Here you may also register your custom route filters.
                    |
                    */
                    App::before(function($request)
                    {
                        //
                    });

                    App::after(function($request, $response)
                    {
                        //
                    });
                    /*
                    |--------------------------------------------------------------------------
                    | Authentication Filters
                    |--------------------------------------------------------------------------
                    |
                    | The following filters are used to verify that the user of the current
                    | session is logged into this application. The "basic" filter easily
                    | integrates HTTP Basic authentication for quick, simple checking.
                    |
                    */
                    Route::filter('auth', function()
                    {
                        if (!Sentry::check()) return Redirect::guest('login');
                    });
                    // Route::filter('auth', function()
                    // {
                    //  if (Auth::guest()) return Redirect::guest('login');
                    // });
                    Route::filter('admin', function()
                    {
                        $user = Sentry::getUser();
                        $admin = Sentry::findGroupByName('Admins');
                        if (!$user->inGroup($admin))
                        {
                            return Redirect::to('login');
                        }
                    });

                    Route::filter('standardUser', function()
                    {
                        $user = Sentry::getUser();
                        $users = Sentry::findGroupByName('Users');
                        if (!$user->inGroup($users))
                        {
                            return Redirect::to('login');
                        }
                    });

                    Route::filter('auth.basic', function()
                    {
                        return Auth::basic();
                    });
                    /*
                    |--------------------------------------------------------------------------
                    | Guest Filter
                    |--------------------------------------------------------------------------
                    |
                    | The "guest" filter is the counterpart of the authentication filters as
                    | it simply checks that the current user is not logged in. A redirect
                    | response will be issued if they are, which you may freely change.
                    |
                    */
                    Route::filter('guest', function()
                    {
                        if (Sentry::check())
                        {
                            // Logged in successfully - redirect based on type of user
                            $user = Sentry::getUser();
                            $admin = Sentry::findGroupByName('Admins');
                            $users = Sentry::findGroupByName('Users');
                            if ($user->inGroup($admin)) return Redirect::intended('admin');
                            elseif ($user->inGroup($users)) return Redirect::intended('/');
                        }
                    });
                    // Route::filter('guest', function()
                    // {
                    //  if (Auth::check()) return Redirect::to('/');
                    // });
                    Route::filter('redirectAdmin', function()
                    {
                        if (Sentry::check())
                        {
                            $user = Sentry::getUser();
                            $admin = Sentry::findGroupByName('Admins');
                            if ($user->inGroup($admin)) return Redirect::intended('admin');
                        }
                    });

                    Route::filter('currentUser', function($route)
                    {
                        if (!Sentry::check()) return Redirect::home();
                        if (Sentry::getUser()->id != $route->parameter('profiles'))
                        {
                            return Redirect::home();
                        }
                    });
                    /*
                    |--------------------------------------------------------------------------
                    | CSRF Protection Filter
                    |--------------------------------------------------------------------------
                    |
                    | The CSRF filter is responsible for protecting your application against
                    | cross-site request forgery attacks. If this special token in a user
                    | session does not match the one given in this request, we'll bail.
                    |
                    */
                    Route::filter('csrf', function()
                    {
                        if (Session::token() != Input::get('_token'))
                        {
                            throw new Illuminate'Session'TokenMismatchException;
                        }
                    });

这是我的show.blade.php:

<!DOCTYPE html>
<html>
<head>
    <title>Muestra un animal</title>
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
@include('../partials/navigation')
<h1>Mostrando vaca con el DIB: {{ $animal->dib }}</h1>
    <div class="jumbotron">
        <p>
            <strong>Sexo:</strong> {{ $animal->sexo }}<br>
            <strong>Registro:</strong> {{ $animal->registro }}<br>
            <strong>DIB:</strong> {{ $animal->dib }}<br>
            <strong>Fecha de nacimiento:</strong> {{ $animal->fecha_de_nacimiento }}<br>
            <strong>Fecha de muerte:</strong> {{ $animal->fecha_de_muerte }}<br>
            <strong>Calificación:</strong> {{ $animal->calificacion }}<br>
            <strong>Fecha de calificación:</strong> {{ $animal->fecha_de_calificacion }}<br>
            <strong>Fecha de baja:</strong> {{ $animal->fecha_de_baja }}<br>
            <strong>Causa de la baja:</strong> {{ $animal->causa_de_baja }}<br>
            <strong>Fecha de transacción:</strong> {{ $animal->fecha_de_transaccion }}<br>
            <strong>Padre:</strong> {{ $animal->padre }}<br>
            <strong>Madre:</strong> {{ $animal->madre }}<br>
            <strong>Observaciones:</strong> {{ $animal->observaciones }}<br>
            <strong>Citogenética:</strong> {{ $animal->citogenetica }}<br>
            <strong>Fecha de la baja:</strong> {{ $animal->fecha_de_baja }}<br>
            <strong>Causa de la baja:</strong> {{ $animal->causa_de_baja }}<br>
            <strong>ADN:</strong> {{ $animal->adn }}<br>
            <strong>Citogenética:</strong> {{ $animal->citogenetica }}<br>
            <strong>Partos:</strong> {{ $animal->partos }}<br>
            <strong>Partos no LG:</strong> {{ $animal->partos_no_lg }}<br>
            <strong>Ganadería:</strong> {{ $animal->ganaderia->nombre }}

        </p>
    </div>
@include('../partials/footer')
</div>
</body>
</html>

这是我的创建视图:

<!DOCTYPE html>
<html>
<head>
    <title>Registrar un animal</title>
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
@include('../partials/navigation')
<!-- will be used to show any messages -->
@if (Session::has('message'))
    <div class="alert alert-info">{{ Session::get('message') }}</div>
@endif
<h1>Registrar un animal</h1>
 @if ($errors->has())
        <div class="alert alert-danger">
            @foreach ($errors->all() as $error)
                {{ $error }}<br>        
            @endforeach
        </div>
        @endif
<!-- FORM STARTS HERE -->
{{ Form::open(['url' => '/animales/create'])}}
        <div>
            {{ Form::label('sexo', 'Sexo:')}}
            {{ Form::select('sexo', array('M'=>'macho', 'H'=> 'hembra', 'C' => 'castrado'))}}
        </div>
        <div>
            {{ Form::label('registro', 'Registro:')}}
            {{ Form::select('registro', array('F'=>'fundacional', 'N'=> 'nacimientos', 'D' => 'definitivo','A' => 'registro auxiliar A', 'B' => 'registro auxiliar B'))}}
        </div>
        <div>
            {{ Form::label('dib', 'DIB:')}}
            {{ Form::input('number', 'dib')}}
        </div>
        <div>
            {{ Form::label('fecha_de_nacimiento', 'Fecha de nacimiento:')}}
            {{ Form::input('date', 'fecha_de_nacimiento')}}
        </div>
        <div>
            {{ Form::label('fecha_de_calificacion', 'Fecha de calificacion:')}}
            {{ Form::input('date', 'fecha_de_calificacion')}}
        </div>
        <div>
            {{ Form::label('calificacion', 'Calificacion:')}}
            {{ Form::input('number', 'calificacion')}}
        </div>
        <div>
            {{ Form::label('padre', 'Padre:')}}
            {{ Form::text('padre')}}
        </div>
        <div>
            {{ Form::label('madre', 'Madre:')}}
            {{ Form::text('madre')}}
        </div>
        <div>
            {{ Form::label('adn', 'ADN:')}}
            {{ Form::text('adn')}}
        </div>
        <div>
            {{ Form::label('fecha_de_transaccion', 'Fecha de transacción:')}}
            {{ Form::input('date', 'fecha_de_transaccion')}}
        </div>
        <div>
            {{ Form::label('observaciones', 'Observaciones:')}}
            {{ Form::textarea('observaciones')}}
        </div>
        <div>
            {{ Form::label('fecha_de_baja', 'Fecha de la baja:')}}
            {{ Form::input('date', 'fecha_de_baja')}}
        </div>
        <div>
            {{ Form::label('causa_de_baja', 'Causa de la baja:')}}
            {{ Form::select('causa_de_baja', array('' => '', 'V'=>'Venta (vida)', 'M'=> 'Muerte', 'S' => 'Sacrificio', 'D' => 'Descalificado', 'N' => 'No asociación'))}}
        </div>
        <div>
            {{ Form::label('partos_no_lg', 'Partos no LG:')}}
            {{ Form::input('date', 'partos_no_lg')}}
        </div>
        <div>
            {{ Form::label('partos', 'Partos:')}}
            {{ Form::input('number', 'partos')}}
        </div>
        <div>
            {{ Form::label('citogenetica', 'Citogenética:')}}
            {{ Form::select('citogenetica', array('' => '', 'L'=>'libre', 'HE'=> 'heterocigoto', 'HO' => 'homocigoto'))}}
        </div>
        <div>
            {{ Form::label('ganaderia_id', 'Ganaderia:')}}
            {{ Form::select('ganaderia_id', $ganaderias)}}
        </div>
        <div>
            {{ Form::label('categoria', 'Categoría:')}}
            {{ Form::select('categoria', array('Listado'=>'En lista', 'Archivado'=> 'Archivado'))}}
        </div>
        <div>
            {{ Form::submit('Registrar animal')}}
        </div>
{{ Form::close()}}
@include('../partials/footer')
</div>
</body>
</html>

谁能帮忙?谢谢!

试试这个:

public function show($id)
{
    $animal = Animal::find($id);         
    $ganaderia = Ganaderia::where( 'id', '=', 'ganaderia_id')->get();
    // show the view and pass the nerd to it
    return View::make('animales.show', array('animal' => $animal, 'ganaderia' => $ganaderia ));
}
所以我

在另一个路由组中调用相同的路由(Route::resource('animales', 'AnimalController');)。一旦我从另一个组中删除了路由,它就起作用了。无论如何,非常感谢所有的帮助!