I am trying to handle a basic form with laravel and am running in to an issue where my POST route isn't being detected and is resulting in a route not defined error in the blade template. My goal is to resolve this error and post the form to the controller, then access the various form fields with the $request param.
This is the error: Route [become-a-customer] not defined.
I appreciate any suggestions on how to resolve this.
Form
<form action="{{ route('become-a-customer') }}" method="post" class="col-md-8 offset-md-2">
<div class="form-row">
<div class="form-group col-md-6">
<label for="first_name">First Name</label>
<input name="last_name" type="email" class="form-control" id="first_name" placeholder="First Name">
</div>
...
</div>
<input type="hidden" name="_token " value="{{ Session::token() }}"/>
<button type="submit" class="btn">SUBMIT</button>
</form>
web.php
Route::post('/become-a-customer', 'BecomeACustomerFormController#postBecomeACustomer');
BecomeACustomerController . php
class BecomeACustomerFormController extends Controller
{
public function postBecomeACustomer(Request $request)
{
$firstName = $request['first_name'];
$lastName = $request['last_name'];
...
...
return redirect()->back();
}
}
Route::post('/become-a-customer', 'BecomeACustomerFormController#postBecomeACustomer')->name('become-a-customer');
use this command
php artisan optimize
In Your blade Template, You have used the Named route for the form action but, it is not specified in the route file (Web.php).
Change your route file like this
Route::post('/become-a-customer', 'BecomeACustomerFormController#postBecomeACustomer')->name('become-a-customer');
OR, you have to change the form action like this
action="{{ url('become-a-customer') }}"
Using the named route is the best practice for a Laravel project.
you can also define as following where "as" key is for naming your route
Route::post('/become-a-customer', ['uses' => 'BecomeACustomerFormController#postBecomeACustomer', 'as' => 'become-a-customer']);
Check your Apache or Nginx configurations. Sometimes a redirect from https to http will alter the method from POST to GET.
I'd recommend setting up a temporary endpoint for GET by the same Route and placing a dd() statement in it to test the theory.
route() method uses route name which is undefined. You can define it via name() method on route as below
Route::post('/become-a-customer', 'BecomeACustomerFormController#postBecomeACustomer')->name('become-a-customer');
for more see doucmentation
For me url('routeName') worked instead of route('routeName')
Related
I want to access my controller and show and log the test messages.
I have been trying to access my controller via a route but it doesn't seem to work.
It's my first time using Laravel so I hope anyone can help I have been struggling with this problem for a couple of hours.
It hasn't been showing error messages, but I can't find any mistakes in my syntax I tried finding if there might be possible problems in my php.ini files, but I don't know.
the form
<form method="post" action="{{ Route('saveItem') }}" accept-charset="UTF-8">
{{ csrf_field() }}
<label for="listItem">New Todo Item</label><br>
<input type="text" name="listItem">
<button type="submit">Save item</button>
</form>
Route
Route::post('/saveItemRoute', [TodoListController::class, 'saveItem'])->name('saveItem');
Controller
class TodoListController extends Controller
{
public function saveItem(Request $request) {
echo("<h1>victory2</h1>");
\Log::debug("TodoListController");
\Log::info(json_encode($request->all()));
$newListItem = new ListItem;
$newListItem->name = $request->ListItem;
$newListItem->is_complete = 0;
$newListItem->save();
return view('welcome');
}
}
After trying again it seem like the rout doesnt function / do anything and i cant figure out why
I know what whent wrong after changes to routing i needed to do this line in the cmd
php artisan route:clear
the first time i tested the routing code but i did not know after changes you need to clear the route
It's not working
showing this
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException The POST method is not supported for this route. Supported methods: PUT, PATCH, DELETE.
<form class="form-ad" action="{{ route('jobs.store') }}" method="post" >
Fake your patch request like this in form tag
<form class="form-ad" action="{{ route('jobs.store') }}" method="post" >
{{ method_field('POST') }} /*here i used post and solved the error*/ /*if you are using form method POST then what is the use of using {{method_field('POST')}} form "store" action? {{method_field('POST')}} is mainly used if you have a PATCH request for update action. Store action is already on POST request in your Routes.*/
<!-- rest of the form -->
</form>
Also, just a suggestions that you can simply make a resource full route.
First make a controller resourceful from artisan command, which will create all the methods required for each method (get, post, patch etc.)
php artisan make:controller Jobs -r
then in your routes/web.php use
Routes::resource('jobs');
You can also view your routes using php artisan command
php artisan route:list
Available Router Methods
The router allows you to register routes that respond to any HTTP verb:
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
https://laravel.com/docs/5.8/routing
add #csrf line in the html view
then it work post method
add #csrf line in the html view
then it work post method
-------------------------------------------------------------------
<form method="post" action="users" class="UserController">
{{method_field('post')}}
#csrf
<input type="text" name="user" placeholder="enter name"><br/><br/>
<input type="password" name="password" placeholder="enter password"><br/><br/>
<button type="submit" value="submit">Submit</button>
</form>
how can i create a folder using a controller function?
(Routes and all are good, but don't know how to make the folder)
I have a simple form for example:
<form method="POST" action="{{ route('admin.projects.store') }}" enctype="multipart/form-data">
<div class="form-group">
<label name="title">Slug:</label>
<input type="text" id="slug" name="slug" placeholder="ejemplo-de-slug" class="form-control form-control-sm">
</div>
</form>
Route in web.php:
Route::post('projects/postUpload', ['uses' => 'AdminController#storeProject', 'as' => 'admin.projects.store']);
I want to put to the folder the same name of the slug.
public function storeProject()
{
return ;
}
Know how to do it?
The best practice is to use Storage:
Storage::makeDirectory($directory);
It will create a new folder on specified disk (local storage, AWS etc).
If you want to create a new folder on the local disk, you can use File facade:
File::makeDirectory('/path/to/directory', 0775);
I'm trying to create a simple search form in Symfony2.
This is my form:
<form action="/search" method="GET">
<div class="input-group">
<input type="text" name="q" class="form-control" placeholder="Search ...">
<span class="input-group-btn">
<button class="btn-u btn-u-lg" type="button"><i class="fa fa-search"></i></button>
</span>
</div>
</form>
So my question is, what is the syntax of the form action? Do I just enter in the exact file that receives it? Or do I need to call some kind of config file?
Another question is how do I handle the search in the controller?
Thanks in advance!
Please note that I'm a total noob in Symfony2 :)
EDIT:
How do I handle the request if I would like a nice url like this: ".../search/value" instead of ".../search?q=value"?
Here is my action:
/**
* #Route("/search/{value}", name="search")
*/
public function searchAction($value)
{
}
in your action you need to put the logical path of your controller to do so call the twig function {{path('you route alias')}} , I assume that you have already set your route configuration.
to handle search in controller that's will depend on you re own logic but in the general case you will have to get the searched word using the request object taht should be some think like that:
public function searchAction(Request $request){
$objet=$request->query->get("word");
//do staff
return $this->render('Your Bundle:views:searchResult.html.twig')
}
In the form action, you need to enter the route that points to your controller. For example if you have a route that is set up to point to "/search" which uses your searchController's search method, you'd have to write action="/search" so when a user submits this form it is going to point to your controller's method.
In your controller you can either grab the $_GET variable with the name of the input (by the way you need to add a name for your input field to be accessible via the request superglobals), or pass the value directly in your url and put an optional variable after your "/search".
Working on a job portal, so I arrived at a point where employers need to edit their posted jobs,On page load it gave me an error Route [employers/job/save/Mw==] not defined, please I need help my deadline is 3hours from now!
Here is my code:
Routes:
//Route for Employer's specified Job Editting -> To get ID as argv
Route::get('employers/job/edit/{id}', 'employerController#editJob');
//Route for Employer's to save specified Job after Editting -> To get ID as argv
Route::post('/employers/job/save/{id}', [
'as' => 'saveJob',
'uses' => 'employerController#saveJob'
]);
View:
{{ Form::open(['action'=>'employers/job/save/'.base64_encode($jobData->id),'class'=>'full-job-form', 'id'=>'jobForm','role'=>'form']) }}
<div class="form-group col-lg-12 col-sm-12 col-md-12">
<label class="sr-only" for="">Job Title</label>
<input type="text" class="form-control"
name="job_title" placeholder="Job Title"
value="{{ $jobData->job_title }}">
<span class="help-block">Eg. Marketing Manager</span>
</div>
Your issue is that you're using the action parameter for your Form::open() call. This expects the name for a controller method (e.g. {{ Form::open(['action' => 'employerController#saveJob']) }}. If you want to link to a pre-generated URL use the url parameter:
{{ Form::open(['url' => 'employers/job/save/'.base64_encode($jobData->id)]) }}
That said, that's not the best practice, as, if you change your routing system, you now have to change all these hardcoded URLs. As such, you should rely on named routing or controller actions.
Now, your route is already named ('as' => 'saveJob') so you should actually use the route parameter of Form::open():
{{ Form::open(['route' => ['saveJob', base64_encode($jobData->id)]]) }}
Alternatively, you could use the action parameter as you are currently trying to do (albeit erroneously):
{{ Form::open(['action' => ['employerController#saveJob', base64_encode($jobData->id)]]) }}
See the docs on forms for more information.
Also, as #TheShiftExchange says, its a bit odd to be using the base 64 encoded id, why not just use the raw id?