vendredi 14 mars 2014

Laravel 4 - username in URL changes to {Username} after pressing submit


Vote count:

0




I'm new to Laravel and am stuck on a syntax issue. I'm trying to make a view that will enable an admin to change the password of a user but when I click submit the page refreshes and the URL has replaced the username (ex: public/users/alex/edit to public/users/{username}/edit). If anyone could explain why this isn't working it'd be greatly appreciated! I made something similar where the users can change their own password and that one seems to be working fine. My only guess is that I'm not carrying over the $username properly but I haven't a clue of how else to do it. Thank y'all so much! Any bit of info helps!


Here is the UserController for the view:



public function getEdit ($username) {
$user = User::whereUsername($username)->first();
return View::make('users.edit', ['user' => $user]);
}

public function postEdit($username){

$validator = Validator::make(Input::all(),
array(
'password' => 'required|min:6',
'password_again' => 'required|same:password'
)
);

if($validator->fails()){
return Redirect::route('user-edit')
->withErrors($validator)
->with('username', $username);
} else {
/*Change password*/
$user = User::whereUsername($username)->first();
$password = Input::get('password');
$user->password = Hash::make($password);
/*password is the field $password is the variable that will be used in the password field*/

if($user->save()){
return Redirect::route('home')
->with('global', 'The password has been changed.');
}
}
return Redirect::route('account-change-password')
->with('global', 'The password could not be changed.');
}


the Route:



/*ADMIN - edit users (GET)*/
Route::get('users/{username}/edit', array(
'as' => 'user-edit',
'uses' => 'UserController@getEdit'
));

/*ADMIN - edit users (POST)*/
Route::post('users/{username}/edit', array(
'as' => 'user-edit-post',
'uses' => 'UserController@postEdit'
));


and the View/Blade:



@extends('layout.main')

@section('content')
<form action="{{ URL::route('user-edit-post') }}" method="post">

<div class="field">
New password: <input type="password" name="password">

@if($errors->has('password'))
{{$errors->first('password')}}
@endif
</div>

<div class="field">
New password again: <input type="password" name="password_again">

@if($errors->has('password_again'))
{{$errors->first('password_again')}}
@endif
</div>


<input type="submit" value="Change Password">
{{ Form::token() }}
</form>
@stop


asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire