I am using Laravel 4's built in password reminder feature in a new application. It seems that the code it's generating and what the docs are saying is either incomplete, inconsistent, or I'm missing a vital point.
So far, I have...
Created Reminder Table
Migrated it
Created Reminder Controller
Tested both get / post for password/remind, successfully.
Where I am stuck is with the reset methods of the controller. The reminder url successfully sends to my email and once I click it, I get a Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException error.
The url that the Password::remind static method sends to my email is http://localhost/application/public/password/reset/3e5e7a5c8562b28909b9948e848c5692dccb4f8a.
My GET / POST reset methods in the reminder controller -
public function getReset($token = null)
{
if (is_null($token)) App::abort(404);
return View::make('password.reset')->with('token', $token);
}
public function postReset()
{
$credentials = Input::only(
'email', 'password', 'password_confirmation', 'token'
);
$response = Password::reset($credentials, function($user, $password)
{
$user->password = Hash::make($password);
$user->save();
});
switch ($response)
{
case Password::INVALID_PASSWORD:
case Password::INVALID_TOKEN:
case Password::INVALID_USER:
return Redirect::back()->with('error', Lang::get($response));
case Password::PASSWORD_RESET:
return Redirect::to('/');
}
}
My password/reset.blade.php file -
{{ Form::open(array('url' => 'password/reset')) }}
{{ Form::hidden('token', $token) }}
{{ Form::email('email', null, array('class'=>'', 'placeholder'=>'Email Address')) }}
{{ Form::password('password', array('class'=>'', 'placeholder'=>'Password')) }}
{{ Form::password('password_confirmation', array('class'=>'', 'placeholder'=>'Password Confirmation')) }}
{{ Form::submit('Reset Password', array('class'=>'')) }}
{{ Form::close() }}
Anyone have any ideas as to where I'm going wrong??
Your most likely missing some routes add these if they don't already exist.
Route::get('password/reset/{token}', 'RemindersController#getReset');
It is also likely that you will need the reset post route as well.
Route::post('password/reset/{token}', 'RemindersController#postReset');
Related
I am just creating a random app to check if the user is eligible for voting or not. I am using middleware to check if age is less than 18 then redirect to /age/validate. But the problem is the middleware is performing action before form submission. Please help.
Form::
{!! Form::open(['action'=>'AgeController#store', 'method'=>'POST']) !!}
{!! Form::number('age', null) !!}
{!! Form::submit('Check') !!}
{!! Form::close() !!}
Middleware::
public function handle($request, Closure $next)
{
if($request->age <= 18){
return redirect()->route('noteligble');
}
return $next($request);
}
Route:
Route::get('/age/validate', function(){
echo 'You are not eligible to vote';
})->name('noteligble');
Route::resource('checkage', 'AgeController')->middleware('agechecker');
Please Help me how i can make middleware work only after form submission.
A middleware is executed before every request. You're assigning the middleware to the whole resource and therefore to the route which displays the form.
The solution
Assign the middleware only to the store method in the controller. Remove the ->middleware('agechecker') method call from your routes file and add assign that in your controller constructor like this:
public function __construct()
{
$this->middleware('agechecker')
->only(['store']);
}
Aloha, I'm making a workout manager in which you have a dashboard displaying your 5 last workouts. I have set a form for each one workout for allowing the user to delete any of them. Here the form in the dashboard:
{!! Form::open(['route' => ['dashboard.workout.destroy', $workout->id], 'style' =>'display:inline-block;', 'method' => 'DELETE']) !!}
This route will call this method in WorkoutController.php
public function destroy($id, Request $request)
{
$workout = Workout::findOrFail($id);
$workout->delete();
$message = "Workout deleted successfully!";
return redirect()->route('dashboard.index', ['message' => $message]);
}
And this route will call this method in DashboardController.php
public function index($message = null)
{
$user = Auth::user();
// Workouts
...
// Inbodies
...
// Measures
...
return view('dashboard.index', compact('user','workoutsDesc','workouts','lastInbody','inbodies','measures','lastMeasure','message'));
}
The question is that I'm trying to pass the variable $message from WorkoutController to DashboardController for displaying a successfull alert after deleting a workout, but I don't know how to do it. I have tried with:
return redirect()->action('Dashboard\DashboardController#index', [$message]);
return redirect()->action('Dashboard\DashboardController#index')->with('message', $message);
return redirect()->route('dashboard.index', $message);
But I still trying to find the way for doing it.
First of all, from Laravel 5.1 Documentation:
If your route has parameters, you may pass them as the second argument to the route method
As the message is not a parameter to your route, so you can't pass that. A possible solution can be Flashing data. Check the next controller if the session has that key and contain a value, then add it to a variable and pass to the view.
Hope this works.
I am new in laravel and I want to update an existing row in database,but when I click on send button in view (for example 127.0.0.1/laravel/public/Article/update/3 ) I encounter the following error:
MethodNotAllowedHttpException in RouteCollection.php line 201:
Here is my code
Route
Route::get('Article/edit/{id}','ArticleController#edit');
Route::get('Article/update/{id}','ArticleController#update');
ArticleController
public function edit($id)
{
$change = Article::find($id);
return view('edit',compact('change'));
}
public function Update($id, Request $request)
{
Article::update($request->all());
return redirect('Article');
}
Model
public $table = 'Article';
protected $fillable = ['title' , 'body'];
edit.blade.php
<h1>ویرایش بست {{$change->title}}</h1>
{!! Form::model($change ,['method'=>'patch' , 'url'=>['Article/update' , $change->id ]]) !!}
{!! Form::label('title','عنوان') !!}
{!! Form::text('title') !!}
<br>
{!! Form::label('body','متن') !!}
{!! Form::textarea('body') !!}
<br>
{!! Form::submit('send') !!}
{!! Form::close() !!}
#if($errors->any())
<ul class ='alert alert-danger'>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
#endif
The easiest way to resolve routing issues with Laravel is to use 'artisan'.
http://laravel.com/docs/5.1/artisan
If you use this command:
php artisan route:list
You'll see every possible route and HTTP verb available for use. Your error is in the RouteCollection so you can always fix these issues by looking at your app/http/routes.php file.
You defined a route as follows:
Route::get('Article/update/{id}','ArticleController#update');
Then you call that route via your form as follows:
{!! Form::model($change ,['method'=>'patch' , 'url'=>['Article/update' , $change->id ]]) !!}
Your routes.php GET definition does not match your form's PATCH method, so you're getting a method not allowed exception because the PATCH route is not defined.
You need this line of code in your routes.php file:
Route::patch('article/update/{id}','ArticleController#update');
I would highly recommend using this instead of defining each method individually:
Route::resource('article', 'ArticleController');
Then run the following command again with artisan to see all routes created:
php artisan route:list
Trying to send an email using Laravel but keep getting a 405 error and the whoops page showing:
* #param array $others
* #return void
*
* #throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
*/
protected function methodNotAllowed(array $others)
{
throw new MethodNotAllowedHttpException($others);
}
The code is:
{{ Form::open(array('url' => 'admin/newemail')) }}
// form items
{{ Form::close() }}
And the route is:
Route::get('admin/newemail', function()
{
$email = 'email#hotmail.com';
$data = array('s' => Input::get('email-heading'));
Mail::send('emails.wereback', $data, function($message) use ($email){
// $message details
});
});
However, if I directly go to the url admin/newemail it works fine.
Any help?
By default the Form helper will generate a html form that uses the POST method. You can specify the method if you need to use a different one:
{{ Form::open(array('url' => 'admin/newemail', 'method' => 'GET')) }}
Or you could also change your route to match POST requests:
Route::post('admin/newemail', function()
{
// [...]
});
This is related to the last question I asked on here where I'm trying to add a new controller to my app that will allow database columns to be edited and updated. I have defined a new controller that will update these edits but when I try to update my edit form, I keep getting a "502 Bad Gateway" error.
edit-album.blade.php:
{{ Form::model($album, array('method' => 'PUT', 'route' => array('edit_album', $album->album_id))) }}
/* Form code here */
{{ Form::close() }}
routes.php:
Route::put('gallery/album/{id}/edit', array('as'=>'edit_album', 'uses'=>'EditAlbumsController#update'));
EditAlbumsController.php:
class EditAlbumsController extends AlbumsController {
public function __construct()
{
parent::__construct();
}
public function update($id)
{
$input = \Input::except('_method');
$validation = new Validators\Album($input);
if ($validation->passes())
{
$album = Album::find($id);
$album->album_name = $input['album_name'];
/* Additional database fields go here */
$album->touch();
return $album->save();
return \Redirect::route('gallery.album.show', array('id' => $id));
}
else
{
return \Redirect::route('gallery.album.edit', array('id' => $id))
->withInput()
->withErrors($validation->errors)
->with('message', \Lang::get('gallery::gallery.errors'));
}
}
Could this be because I have a bad route or it is not defined properly?
I did run 'composer dumpautoload -o' after making some changes in the code as per a suggestion I found online, not sure if this had an effect.
This issue was being caused by a bad route and therefore the browser produced a '502 Bad Gateway error'.