Laravel Middleware Performing Action Before Form Submission - php

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

Related

Laravel 5.4 post redirect to GET status 302

I have a simple post form in Laravel that's is being redirecting to the same form after the post (with status 302). I'm not sure if it's any route problem or something different.
my form:
{!! Form::open(['route' => 'subscription.store']) !!}
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_xxxxxx"
data-amount="2000"
data-name="Name"
data-description="2 widgets"
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
data-locale="auto"
data-currency="cad">
</script>
{!! Form::close() !!}
Route:
Route::post('/subscription/store', 'Admin\SubscriptionController#store')->name('subscription.store')->middleware('can:admin-access');
Cntroller:
public function store(Request $request)
{
$user = User::find($request->user()->id);
$user->newSubscription('main', 'main')->create($request->input('stripeToken'));
if ($user->subscribed('main')) {
return redirect()->route('admin.dashboard');
}
}
Chrome network log:
Request Method:POST
Status Code:302 Found
Remote Address:10.1.1.33:80
Referrer Policy:no-referrer-when-downgrade
Any suggestions what's missing or wrong here?
You need to define a default redirection. Currently you only redirect when the user is subscribed. Set a default route to redirect in case if they aren't.
public function store(Request $request)
{
$user = User::find($request->user()->id);
$user->newSubscription('main', 'main')->create($request->input('stripeToken'));
if ($user->subscribed('main')) {
return redirect()->route('admin.dashboard');
}
return redirect()->route('admin.dashboard');
}
It was my mistake. I have a middleware that's responsible to check the subscription status in every request and I had by mistake enabled in my constructor.

Passing argument from controller to controller not working

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.

How to update exsiting row in database in laravel5

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

Laravel 4: Unable to generate a URL, as such route does not exist

Here is my routes.php:
Route::group(['prefix' => 'mine'], function () {
Route::get('/first', ['as' => 'mine.first', 'uses' => 'MyApp\Controllers\MyController#first']);
});
Here is my HTML/Twig file:
{{ form_open({'action': 'mine.first'}) }}
{{ form_submit('Start') }}
{{ form_close }}
And here is my controller:
class MyController extends BaseController {
public function first()
{
\View::make('stuff.mine.first'); //in folder app/views/stuff/mine
}
}
The error is "An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "MyController#first" as such route does not exist.") in "stuff.show" at line 130."
All the answers on this topic that I've seen are to name the route, but I've already done that.
Also, when I go to the URL manually (localhost/mine/first), the screen is blank even though there is HTML in that file.
Any idea what's going on? Thanks.
mine.first is a route name, not an action.
Use:
{{ form_open({'route': 'mine.first'}) }}
As for the view, controller action need to return a Response (the View generates one), so you just need add the proper keyword:
public function first()
{
return \View::make('stuff.mine.first');
}

Laravel 4 Authentication: Reminders Controller - GET reset not found

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

Categories