Route [name]未定义。 laravel 4.1

时间:2014-08-31 16:19:11

标签: laravel laravel-routing

我在laravel 4.1中遇到了一些问题

我在url有一个页面:/ AppFastFood / webapp / public / users / profile 谁工作tu编辑和更新当前用户。

我有一个管理用户的页面,索引在网址上很好用: / AppFastFood / web应用/公共/用户

仅更改用户角色的页面是/ AppFastFood / webapp / public / users / 1 / edit 并且无法显示此页面,我有以下错误: Route [users.updaterole]未定义。 (查看:C:\ wamp \ www \ AppFastFood \ webapp \ app \ views \ users \ edit.blade.php)

有人可以帮我吗?

这里是我的代码: routes.php文件

    <?php


    Route::get('/', function()
    {
        return Redirect::to('/users/dashboard');

    });

    //Users
    Route::get('users/{all}/edit', 'UsersController@edit');

    Route::post('users/{all}', 'UsersController@updaterole');

    Route::controller('users', 'UsersController');

    //Password
    //Route::controller('password', 'RemindersController');


    // Routes Users
    Route::get('/remind','RemindersController@getRemind');
    Route::post('/remind','RemindersController@postRemind');
    Route::get('/password/reset/{token}','RemindersController@getReset');
    Route::post('/password/reset','RemindersController@postReset');


    //requière un login
    Route::group(array('before' => 'auth'), function () {
    // Routes POI
        Route::resource('pois', 'PoiController');
        Route::resource('categories', 'CategoryController');
        Route::resource('users', 'UsersController');
        //Route::resource('users.updaterole', 'UsersController@updaterole');


    });

UsersController.php         

    class UsersController extends BaseController {
            //protected $layout = "layouts.main";

        public function __construct() {
             $this->beforeFilter('csrf', array('on'=>'post'));
             $this->beforeFilter('auth', array('only'=>array('getDashboard')));
        }

        public function edit($id)
        {
            $user = User::find($id);

            $roles = Role::getList(false);


            $this->layout->content = View::make('users.edit', array('user' => $user,'roles'=> $roles));
            //$this->layout->content = View::make('users.edit')->with('user', $user);
        }

        public function index()
        {

            $users = User::all();

            // load the view and pass the nerds
                $this->layout->content = View::make('users.index')->with('users', $users);
        }



        public function updaterole($id)
        {



                // store
                /*$user = User::find($id);

                $user->fk_role    = Input::get('fk_role');

                $user->save();



                redirect
                */
                Session::flash('message', 'Successfully updated user !');
                return Redirect::to('users');
                //return $this->postUpdate($id);        

        }



        public function getRegister() {
            $this->layout->content = View::make('users.register');
        }

        public function postCreate() {
             $validator = Validator::make(Input::all(), User::$rules);

            if ($validator->passes()) {
                        $user = new User;
                        $user->firstname = Input::get('firstname');
                        $user->lastname = Input::get('lastname');
                        $user->username = Input::get('username');
                        $user->email = Input::get('email');
                        $user->password = Hash::make(Input::get('password'));
                        $user->fk_role=3;
                        $user->save();

        return Redirect::to('users/login')->with('message', 'Thanks for registering!');
                } else {
                  return Redirect::to('users/register')->with('message', 'The following errors occurred')->withErrors($validator)->withInput();   
                }
        }
        public function getLogin() {

            if(Auth::user()){
                $this->layout->content = View::make('users.dashboard');
            }else{
                $this->layout->content = View::make('users.login');     
            }
        }

        public function postSignin() {
             if (Auth::attempt(array('email'=>Input::get('email'), 'password'=>Input::get('password'))) || Auth::attempt(array('username'=>Input::get('email'), 'password'=>Input::get('password')))) {
                return Redirect::to('users/dashboard')->with('message', 'You are now logged in!');
            } else {
                return Redirect::to('users/login')
                    ->with('message', 'Your username/password combination was incorrect')
                    ->withInput();
                }    
        }

        public function getDashboard() {
            $this->layout->content = View::make('users.dashboard');
        }
        public function getLogout() {
            Auth::logout();
            return Redirect::to('users/login')/*    ->with('message', 'Your are now logged out!')*/;
        }

        public function getRememberToken()
        {
        return $this->remember_token;
        }

        public function setRememberToken($value)
        {
            $this->remember_token = $value;
        }

        public function getRememberTokenName()
        {
            return 'remember_token';
        }

        ///update profile
        public function getProfile() {  
            $this->layout->content = View::make('users.profile');
        }

            public function postUpdate($onlyupdaterole = false) {
        $validator = Validator::make(Input::all(), User::$rulesedituser);

        if ($validator->passes()) 
               {
               $user = User::find(Auth::user()->id);
               $currentemail = $user->email;
               $user->firstname = Input::get('firstname');
               $user->username = Input::get('username');
               $user->lastname = Input::get('lastname');
               $user->email = Input::get('email');
               if(Input::get('password') != NULL)
               {

                    if(Auth::attempt(array('email'=>$currentemail, 'password'=>Input::get('oldpassword'))))
                    {
                       $user->password = Hash::make(Input::get('password'));
                    }
                    else
                    {
                        return Redirect::to('users/profile')->with('alert', 'your actual password is not correct');
                    }             
               }

               /// vérification que l'email entrée ne pas déjà utilisée.
               $existinguser = New User;
               // on selectrionne l'utilisateur qui a la même add email que celle entrée.
               $existinguser = User::where('email', '=', $user->email)->first();
               if ($existinguser != null){
                   if ($existinguser->id != $user->id) {
                        return Redirect::to('users/profile')->with('message', 'the e-mail address entered is already in use, please use another one');
                   }
                }
                //fin vérif mail

                ///// vérification que l'username entrée ne pas déjà utilisée.
                 $existinguser = New User;
               // on selectrionne l'utilisateur qui a la même add email que celle entrée.
               $existinguser = User::where('username', '=', $user->username)->first();
               if ($existinguser != null){
                   if ($existinguser->id != $user->id) {
                        return Redirect::to('users/profile')->with('message', 'the username  entered is already in use, please use another one');
                   }
                }

                //fin verif username

               $user->save();
                  // validation has passed, save user in DB
                    return Redirect::to('users/dashboard')->with('message', 'Modification OK !');
                } 
                else 
                {
                    return Redirect::to('users/profile')->with('message', 'The following errors occurred')->withErrors($validator)->withInput();
                }
        }


    }
    ?>

edit.blade.php

@section('content')
             <h1>Edit {{ $user->firstname }}</h1>

        <!-- if there are creation errors, they will show here -->
        {{ HTML::ul($errors->all()) }}

        {{ Form::model($user, array('route' => array('users.updaterole', $user->id), 'method' => 'PUT')) }}


    <div class="form-group">
            {{ Form::label('fk_role', 'Role') }}

            {{Form::select('fk_role',$roles, $user->fk_role, array('class' => 'form-control')) }}
        </div>


            {{ Form::submit('Edit the user !', array('class' => 'btn btn-primary')) }}

        {{ Form::close() }}

    @stop   

路线: routes

2 个答案:

答案 0 :(得分:0)

您的路线声明存在问题。您已使用相同的url和控制器名称声明了两个路由,如下所示:

Route::controller('users', 'UsersController');
Route::resource('users', 'UsersController');

使用任何一个。在php artisan routes命令的结果中,您在表单中使用的路线不是users.updaterole.update。尝试使用任何一个或将其重命名为不同的路由和控制器。

答案 1 :(得分:0)

谢谢,我用以下路线解决了我的问题:

Route::get('usersadmin/{all}/edit', 'UsersController@edit');
Route::get('usersadmin.update', 'UsersController@update');
Route::post('usersadmin/{all}', 'UsersController@update');