Page not found in Laravel, routes problem - php

I have a problem with my Laravel and probably routes but I'm not sure.
So, there is a contact form in modal window. When I click on Send button I've got page not found error.
What I have in the web.php is
Route::post('/apply_now','HomeController#apply_now')->name('apply_now');
In the HomeController.php
public function apply_now(Request $request)
{
... form fields data
return Redirect::to('/')->with('message', 'Sent');
}
And the form
{{Form::open(array('route'=>'apply_now','files' => true,'method'=>'post'))}}
...
form field
{{Form::close()}
The error
Not Found
The requested URL /apply_now was not found on this server.
I don't see anything wrong with the routes but yet can't find the problem.
UPDATE:
| | POST | apply_now | apply_now | App\Http\Controllers\HomeController#apply_now
UPDATE 2. The modal
<!-- Apply Modal -->
<div class="modal fade" id="apply" tabindex="-1" role="dialog" aria-labelledby="applyModalLable">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title text-center" id="applyModalLable">Apply Now</h4>
</div>
<div class="modal-body">
<form method="POST" action="https://example.com/apply_now" accept-charset="UTF-8" enctype="multipart/form-data">
<input name="_token" type="hidden" value="OyEdnHIWRgbZmPo0joodNmWraDSuuACIrwqup044">
<div class="form-group ">
<input type="text" name="your_name" class="form-control" placeholder="*Your Name" value="" >
</div>
<div class="form-group ">
<label>*Country</label>
<input type="text" name="country" class="form-control" placeholder="Your Country" >
</div>
<div class="form-group ">
<input type="text" name="contact_email" class="form-control" placeholder="*Contact Email" >
</div>
<div class="form-group ">
<input type="text" name="contact_phone" class="form-control" placeholder="*Contact Phone">
</div>
<div class="form-group text-center">
<button type="submit" class="btn btn-custom btn-sm btn-block">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>

make routes like this:
Route::match(['get', 'post'],'/apply_now', 'HomeController#apply_now');

Looks like there is no error in the code, can you clear the cache;
php artisan route:cache
enter image description here
Did you see the area in the picture?

Try Adding
{{ csrf_field() }}
This will add the CRSF Token field to your form
eg. <input type="hidden" name="_token" value="SomeRandomString">
which is required by laravel ,CSRF is enabled by default on all Routes to check if the post request is secure , you can also disable it from VerifyCsrfToken.php which is middleware located at
app\Http\Middleware
To Disable the CRSF for your route
update
protected $except = [
//
'apply_now'
];
Disabling this is not a good practice, If you want your application secure
Add
{{ csrf_field() }}
In your form for.eg.
{{ Form::open(array('route'=>'apply_now','files' => true,'method'=>'post')) }}
...
form field
{{ csrf_field() }}
{{ Form::close() }}
Now once you submit the form laravel will check if crsf token is sent with the form and let your request proceed further

Try with this,
Route::get('/', 'HomeController#index')->name('home');
In your controller
return redirect()->route('home')->with('message', 'Sent');
Hope this helps :)

Run this command and try again
php artisan optimize:clear
or
remove all files from the
/bootstrap/cache
/storage/framework/cache/data
/storage/framework/sessions
/storage/framework/views
and make sure you have defined both urls into web.php file something like this:
Route::post('/','HomeController#index');
Route::post('apply_now','HomeController#apply_now')->name('apply_now');
and make sure your server has turned on mod rewrite. So Laravel can handle .htaccess rules.

Related

From validation Throws error The GET method is not supported for this route. Supported methods: POST."

i am new to laravel..Kind of stuck at this place. Tried many solutions for this but none worked yet, There are similar question but most unresolved, or proper evident solution not posted yet(from google,stackoverflow ..etc)
i have defned a custom route
Route::post('/ComplaintGenerate', 'ComplaintsController#generate');
whenever i submit the view with 'POST' method as
<form action="/ComplaintGenerate" method="POST" >
without any validation rule in my Complaintscontroller everything works fine and i can save data. but when i put validation either through Requests or direct it throws error Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.
if i remove validation everything works fine. I also tried with GET method but still dint work.
A little peace of advice will be very much appreciated.
Web.route
Route::middleware(['auth'])->group(function(){
Route::post('/Complaint', 'ComplaintsController#find');
Route::post('/ComplaintGenerate', 'ComplaintsController#generate');
Route::post('/Complaint/{Complaint}', 'ComplaintsController#save_customer');
Route::resource('Complaints', 'ComplaintsController');
Route::resource('Occupancies', 'OccupanciesController');
Route::resource('Customers', 'CustomersController');
Route::resource('Services', 'ServiceController');
Route::resource('ServiceTeams', 'ServiceTeamController');
Route::get('/home', 'HomeController#index')->name('home');});
My controller:
public function generate(GenerateInitialComplaintRequest $request)
{
$complaint = Complaint::find($request->complaint_id);
$complaint->update([
'complaint_date'=>$request->complaint_date,
'complaint_description'=>$request->complaint_description,
]);
return redirect(route('Complaints.index')->with('complaint', Complaint::all()));
}
my View:
<div class="container my-5">
<div class="col d-flex justify-content-center my-4">
<div class="card">
<div class="card-header">
<form action="/ComplaintGenerate" method="POST" >
#csrf
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<div class="form-row">
<div class="form-group col-md-6">
<label for="complaint_id">Complaint Number</label>
<input type="text" class="form-control" id="complaint_id" name="complaint_id" value="{{$complaint->id}}" readonly >
</div>
<div class="form-group col-md-6">
<label for="complaint_date">Complaint Date</label>
<input type="text" class="form-control" id="complaint_date" name="complaint_date">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="complaint_description">Complaint Description</label>
<textarea class="form-control" id="complaint_description" name="complaint_description" rows="5"></textarea>
</div>
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
What is the route for displaying your form? When validation fails, Laravel makes redirection using GET method to the route it was displayed from.
I assume the form might be displayed in the find method of your ComplaintsController, and when validation fails, there's redirection to this route and that is what throws an error.
Can you also show your validation methods and what data are you trying to send through form?
i found the solution as mentioned by Ankur Mishra and Aryal,
We have to remember as mentioned by Aryal When validation fails, Laravel makes redirection using GET method to the route it was displayed from. And i displayed my form through below
Route::post('/Complaint/{Complaint}', 'ComplaintsController#save_customer');
Controller method:
public function save_customer($id)
{
$complaint = Complaint::create([
'customer_id'=>$id
]);
// $complaint = Complaint::whereCustomer_id($id)->firstorfail();
return view('complaints.initial_complaint')->with('complaint', $complaint);
}
'complaints.initial_complaint' is the view which has the form which gave me the error of
The GET method is not supported for this route. Supported methods: POST. on submission
So i change POST route to GET :-
Route::middleware(['auth'])->group(function(){
//Route::resource('Complaints', 'ComplaintsController');
Route::get('/Complaint', 'ComplaintsController#find');
Route::get('/Complaint/{Complaint}', 'ComplaintsController#save_customer');
Route::get('/ComplaintGenerate', 'ComplaintsController#generate');
Route::resource('Complaints', 'ComplaintsController');
Route::resource('Occupancies', 'OccupanciesController');
Route::resource('Customers', 'CustomersController');
Route::resource('Services', 'ServiceController');
Route::resource('ServiceTeams', 'ServiceTeamController');
Route::get('/home', 'HomeController#index')->name('home');
});
and in view i passed GET as hidden method
<form action="/ComplaintGenerate" method="POST" >
#csrf
#method('GET')
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<div class="form-row">
<div class="form-group col-md-6">
<label for="complaint_id">Complaint Number</label>
<input type="text" class="form-control" id="complaint_id" name="complaint_id" value="{{$complaint->id}}" readonly >
</div>
<div class="form-group col-md-6">
<label for="complaint_date">Complaint Date</label>
<input type="text" class="form-control" id="complaint_date" name="complaint_date">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="complaint_description">Complaint Description</label>
<textarea class="form-control" id="complaint_description" name="complaint_description" rows="5"></textarea>
</div>
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
and now it is working me.. Just posted so if anybody could use it for future reference
you should add
Route::get('/ComplaintGenerate', 'ComplaintsController#generate');
Route::post('/ComplaintGenerate', 'ComplaintsController#generate');

Laravel - CSRF is not matching with session token

I am trying to submit one form and login one user and it seems to work fine in localhost, but when I upload this code onto a live server then the Token is not working.
I have checked that the CSRF token and session token value is not same.
Also the post request is not working so I have added the route in except VerifyCsrfToken now the post request is working but after login the user session is not working
if (Session::token() != $request->get('_token'))
{
echo 'Not Match';exit;
}
Above the result shows me no match .
I have regenerated the key on the server, also I had provided the permission to storage folder.
Cache data has been cleared but the authectioncation is not working it doesn't work.
<form class="login-form js-login-frm" role="form" method="POST" action="{{ route('admin.login-post') }}">
{{ csrf_field() }}
<div class="form-body">
<div class="row">
<div class="col-xs-12">
<div class="form-group">
<div class="input-group" title="{{__('username_or_email_address')}}">
<span class="input-group-addon"><i class="fa fa-envelope-o"></i></span>
<input class="form-control" autofocus type="text" id="email_address" placeholder="{{ __('username_or_email_address') }}" name="login"/>
</div>
</div>
</div>
<div class="col-xs-12">
<div class="form-group">
<div class="input-group" title="{{__('enter_password')}}">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input class="form-control" id="password" type="password" placeholder="{{ __('enter_password') }}" name="password"/>
</div>
</div>
</div>
</div>
</div>
<div class="form-actions">
<button class="btn-del btn-5 btn-5a fa fa-lock login-btn" type="submit" id="login_btn">
<span>{{ __('login') }}</span>
</button>
</div>
</form>
In My controller
$remember = $request->remember ? true : false;
$auth = Auth::guard('web')->attempt([
'email' => $request->login,
'password' => $request->password,
'is_active' => 1
], $remember);
This code is working fine but the user doesn't get redirected to the dashboard page it redirect to login page again.
Can anyone help with the login the user in the system, and redirect to proper route.
In a test environment the CSRF token is not checked in the middleware. Replace {{ csrf_field() }} with #csrf and you should be fine. Have a look at the source code of the generated login page.

how to pass value from input form into another page in laravel

Excuse me, i'm new in learning about Laravel and I have a problem about show data value from form input.
I have create.blade.php :
#extends('layouts.master')
#section('content')
<div class="container">
<div class="header">
<h1><b>Create an account</b></h1>
<h5>Welcome to Konoha Village</h5>
</div>
{{ csrf_field() }}
#if(isset($name))
<div class="alert alert-warning alert-dismissible" role="alert">
Halo <strong>{{$name}}</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">$times;</span>
</button>
</div>
#endif
<div class="form">
<form action="{{ url('final-test') }}" method="post" id="form1">
<div class="form-group">
<input name="name" id="name" class="form-control" placeholder="Your Full Name"/>
</div>
<br>
<div class="form-group">
<input style="cursor:pointer" type="submit" class="btn btn-primary" id="submit" value="Show into Dashboard">
</div>
<div class="form-group">
</div>
</form>
</div>
</div>
#endsection
and my controller with name AccController.php :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AccController extends Controller
{
public function create() {
return view('home.create');
}
public function show(Request $r) {
$soul = $r ->name;
$pesan = "Your name is {$r->name}";
}
}
and my route in web.php :
//route to get play form
Route::get ('start', 'AccController#create' )->name('home.create');
Route::post('final-test', 'AccController#show');
i want to show in another page view that i called show.blade.php :
#extends('layouts.master')
#section('content')
<div>
{{$name = Input::get('name')}}
<h1>Your name is {{ $pesan }}</h1> </div>
#endsection
nothing's error in the end but it couldn't show the value from the input form, would you help me please?
Regards, Aga.
Your web.php:
//route to get play form
Route::get('/start', 'AccController#create')->name('home.create');
Route::post('/final-test', 'AccController#show')->name('home.show');
Added a name home.show to the route final-test.
Your AccController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AccController extends Controller
{
public function create()
{
return view('home.create');
}
public function show(Request $r)
{
$soul = $r->name;
return view('home.show')->with('soul', $soul);
}
}
Your show.blade.php:
#extends('layouts.master')
#section('content')
<div>
<h1>Your name is {{ $soul }}</h1>
</div>
#endsection
Your create.blade.php:
#extends('layouts.master')
#section('content')
<div class="container">
<div class="header">
<h1><b>Create an account</b></h1>
<h5>Welcome to Konoha Village</h5>
</div>
#if(isset($name))
<div class="alert alert-warning alert-dismissible" role="alert">
Halo <strong>{{$name}}</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">$times;</span>
</button>
</div>
#endif
<div class="form">
<form action="{{ route('home.show') }}" method="post" id="form1">
{{ csrf_field() }}
<div class="form-group">
<input name="name" id="name" class="form-control" placeholder="Your Full Name"/>
</div>
<br>
<div class="form-group">
<input style="cursor:pointer" type="submit" class="btn btn-primary" id="submit" value="Show into Dashboard">
</div>
<div class="form-group">
</div>
</form>
</div>
</div>
#endsection
Here's where the most important change has been made: I moved {{ csrf_field() }} inside the form, so you don't get The page has expired due to inactivity. Please refresh and try again. Also changed the form action to the named route {{ route('home.show') }}.
I kept your <span aria-hidden="true">$times;</span> but this will only show $times;, might need to tweak that.
You are not following conventions here. show method is to data from database. In order to show form data you have do it in store method
HTML Code
There should be some minor changes in html form
{{--Changing in just action--}}
{{-- If it doesn't accept that action then replace it with {{AccController.php#store}} --}}
<form action="AccController.php#store" method="post" id="form1">
<div class="form-group">
<input name="name" id="name" class="form-control" placeholder="Your Full Name"/>
</div>
<br>
<div class="form-group">
<input style="cursor:pointer" type="submit" class="btn btn-primary" id="submit" value="Show into Dashboard">
</div>
<div class="form-group">
</div>
</form>
Then in Controller you will get that form data in store function
public function store(Request $r) {
$soul = $r ->name;
$pesan = "Your name is {$r->name}";
return $pesan;
}
How to get it in route?
Well you are doing some mistakes in route.php. You just have to follow conventions which laravel provides us.
Replace your route code with this.
Route::resource('/posts', 'AccController.php');
Here it will automatically call all by default functions of Controller and assign them particular routes. For example Below the picture
You just have to type php artisan route:list in terminal or command prompt and you will see list of routes which you have created and which laravel creates for you along with method, URI and name. You just have to follow conventions and it will give you results automatically
Give it a try and tell me

Laravel 5 Build url with Laravel Form Get method

I'm trying to create a form that passes data via get to the controller but the URL looks allways like this:
http://example.com/test?_token=VinwWFxKIhKvMqrrEBN5xwXhrmYQjLnOWV8s7dht&param1=horse&param2=cat&param3=dog
But I want something like this:
http://example.com/test/param1=horse/param2=cat/param3=dog
or
http://example.com/test/horse/cat/dog
Route:
Route::get('test/{param1}/{param2}/{param3}', ['as' => 'test', 'uses' => 'MainController#test']);
HTML:
<form action="{{ route('test') }}" method="get">
{{ csrf_field() }}
<div class="col-md-3">
<div class="form-group">
<label for="animal1">animal1</label>
<br>
<input type="text" name="animal1" class="form-control">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="animal2">animal2</label>
<input type="text" name="animal2" class="form-control">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="animal3>animal3</label>
<input type="text" name="animal3" class="form-control">
</div>
</div>
<div class="col-md-12">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
The problem is that the test route is reloaded every 10 seconds. Therefore, the form values must be in the URL so that I can process them correctly in the controller.
I've found this question here but that wasn't so helpful
How To Pass GET Parameters To Laravel From With GET Method ?
Thanks for your help!
To do this, you'll need to switch off CSRF token check, which is a bad idea. Or you could use JS to build the query which is not a good idea too.
The best way to handle this is to use POST instead of GET:
<form action="{{ route('test') }}" method="post">
And then change the route to:
Route::post('test', ['as' => 'test', 'uses' => 'MainController#test']);
You can do this via javascript.
You don't need to use form like this.you can just get input values by id(getElementById) and on a button click, format them as you expect (test/{param1}/{param2}/{param3}) and redirect page to that.

Laravel testing phpunit press NotFoundHttpException

I'm trying to setup phpunit tests for a project with Laravel 5.1.40 (LTS), php 5.6.28, and phpunit 4.8.27. I'm sorry if this issue has been solved before, but I couldn't find anything.
public function testAdminLogin()
{
$this->visit('/auth/login')
->type('email#address.com', 'email')
->type('1234567890', 'password')
->press('Login');
}
There seem to be an issue with press('STRING') with both <button> and <input> as submit buttons. Below is the error message I receive.
1) ExampleTest::testAdminLogin
A request to [http://localhost/auth/login] failed. Received status code [500].
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:165
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:63
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:85
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:684
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:671
C:\xampp\htdocs\project\tests\ExampleTest.php:52
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:176
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:129
However, when I change the <button> tag to an <a> tag, add an id to it, and replace the press(STRING) function with the click(ID) function, the test passes. I could change the <button> to an <a>, but that would only a temporary fix, and future cases might not allow the tag change.
Below is the HTML form with the <button> tag.
<form action="/auth/login" method="POST" class="form-horizontal">
<div class="form-group">
<label for="email" class="col-sm-4 control-label">E-Mail</label>
<div class="col-sm-6">
<input type="email" name="email" class="form-control" value="{{ old('email') }}" autocomplete="off">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-4 control-label">Password</label>
<div class="col-sm-6">
<input type="password" name="password" class="form-control" autocomplete="off">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-6">
<button type="submit" class="btn btn-default btn-login">Login</button>
</div>
</div>
</form>
You said you define auth routes manually. In this case you should have POST route for sending login form:
Route::post('auth/login', ....
It works in a href because it sends GET request for which you have route. Form sends POST request by default.

Categories