Laravel 4 URL::action error - php

I have a controller called CharactersController.php in my controllers directory. Here are the two functions:
public function search()
{
return View::make('search.search');
}
public function post_search()
{
$name = Input::get('character');
$searchResult = Player::where('name', 'LIKE', '%'.$name.'%')->paginate(5);
return View::make('search.post_search')
->with('name', $name)
->with('searchResult', $searchResult);
}
In the first function (function search()) I return a view. Here's the code of the view(just the form):
<form id="custom-search-form" class="form-search form-horizontal pull-right" action="{{ URL::action('CharactersController#post_search') }}" method="get">
<div class="input-append spancustom">
<input type="text" class="search-query" name="character" placeholder="Character/guild name">
<button type="submit" class="btn"><i class="icon-search"></i></button>
</div>
</form>
When I try to run the form (to search) I get an Unknown action [CharactersController#post_search]. error. I had this error before, I tried switching controllers, tried doing everything. But it didn't work. So I gave up.
Anyone who can solve it?

Is that controller RESTful?
And Have you created a Route::controller() in the routes.php?
If it's not RESTFul, can you try removing post_ in the method's title?

Have you actually defined a route for the controller's method? You need to do that, else that pesky exception will be thrown when you call URL::action()

Function names need to be camelCased per PSR-1. Chnage post_search to postSearch

Related

error: The POST method is not supported for this route. Supported methods: GET, HEAD. - using laravel livewire

I'm trying to do an image upload using Laravel livewire, but when I click on the button "upload" to test the functionality this error appears
The POST method is not supported for this route. Supported methods: GET, HEAD'
The programs:
ROUTE
Route::get('/upload', UploadFoto::class)->name('upload.foto.user');
CONTROLLER (using dd for the tests)
<?php
namespace App\Http\Livewire\User;
use Livewire\Component;
class UploadFoto extends Component
{
public $foto;
public function render()
{
return view('livewire.user.upload-foto');
}
public function storageFoto()
{
dd('aqui');
}
}
VIEW
#extends('layouts.app')
#section('content')
<div>
{{-- To attain knowledge, add things every day; To attain wisdom, subtract things every day. --}}
<form action="#" method="post">
<input type="file" name="foto" id="foto" wire:submit.prevent="storageFoto">
<button type="submit">Upload</button>
</form>
</div>
#endsection
You set get method on this route - but upload use post method. Change it:
Route::post('/upload', UploadFoto::class)->name('upload.foto.user');
please check this, you added submit in wrong place of form
<div>
<form wire:submit.prevent="storageFoto" method="post">
<input type="file" name="foto" id="foto">
<button type="submit">Upload</button>
</form>
</div>
and you should check this lines into app file
#livewireStyles
#livewireScripts
change route method to Post
Route::post('/upload', UploadFoto::class)->name('upload.foto.user');

How to add a custom method to resource controller in Laravel 5.6

What is the correct way of adding a custom method to a resource controller in Laravel 5.6?
What I have so far is a new method in my ProfileController:
public function approve($id){
$user = User::find($id);
$user->state = '1';
$user->save();
return redirect('/dashboard')->with('success', 'User approved.');
}
As well as the following lines added to my web.php file:
Route::post('/profile/{$id}/approve', 'ProfileController#approve');
Route::resource('profile', 'ProfileController');
The form in my view is (afaik) correctly rendered to:
<form method="POST" action="http://myurl.com/profile/10/approve" accept-charset="UTF-8">
<input name="_token" type="hidden" value="v3F1RRhi7iJL2o4egOhcRiuahaGQBwkGkfMal1lh">
<input name="_method" type="hidden" value="PATCH">
<input class="btn btn-success" type="submit" value="Approve User">
</form>
Unfortunately nothing happens, except the "Sorry, the page you are looking for could not be found." page to be shown.
What am I missing? And to expand a bit on this question also, is this even a valid way to implement "single field updates" on a db entry?
Thank you for your help!
i see you have two problems:
firstly correct the route like that
Route::post('/profile/{id}/approve', 'ProfileController#approve');
secondly you have to delete
<input name="_method" type="hidden" value="PATCH">
or replace your route like that:
Route::patch('/profile/{id}/approve', 'ProfileController#approve');
You would want to remove the $ sign from your route:
Route::post('/profile/{id}/approve', 'ProfileController#approve');
The rest of it is correct.
You have written the parameter like var: $id, and you may write it without '$'.
But really you can use the Laravel implicit model binding function to do this:
Route::post('/profile/{user}/approve', 'ProfileController#approve');
And then in your controller:
public function approve(User $user){
// Delete this line--> $user = User::find($id);
$user->state = '1';
$user->save();
return redirect('/dashboard')->with('success', 'User approved.');
}

Passing variable from button to controller Laravel

I am having a little routing problem in Laravel 5.2. I have a result page which shows detailed information about personnel. I would like a button, which when enabled, generates a PDF page. Passing the variables has been a problem but I am very close now! I will public my code to elaborate.
result page
<form action="generatePDFpage" method="get">
<button type="submit" class="btn btn-default">Generate PDF!</button>
</form>
routes.php
Route::get('/dashboard/result/generatePDFpage', 'resultController#GeneratePDFc');
GeneratePDFc controller
public function GeneratePDFc(){
$id_array_implode = "HALLO";
$pdf= PDF::loadView('GeneratePDF', ["test"=>$id_array_implode])->setPaper('a4', 'landscape');
return $pdf->stream('invoice.pdf');
}
So, on the result page I am using a array ($id_array) to search the database for the matching records. I need to pass this variable onto the GeneratePDFc controller, so that I can pass that again to the loadView function!
Could someone please help me out? :-)
When you're using get method, you can do just this:
<a href="{{ route('route.name', $parameter) }}">
<button type="submit" class="btn btn-default">Generate PDF!</button>
</a>
For other methods you can use something like this (this one is for DELETE method):
<form method="POST" action="{{ route('route.name', $parameter) }}" accept-charset="UTF-8">
<input name="_method" type="hidden" value="DELETE">
{{ csrf_field() }}
<button type="submit" class="btn btn-sm btn-default">Generate PDF!</button>
<input type="hidden" value="someVariable" />
</form>
To get variable, use something like this:
public function generatePDF(Request $request)
{
$someVariable = $request->someVariable;
I don't know Laravel but I think when in your action="" of the form you can put your route with its parameters no ?
I've found it here : https://laravel.com/docs/4.2/html#opening-a-form
And access the variable in your controller using the $request var

How do i use to the input value in the form in the where clause of my model using codeigniter

Want to use the luggage_id in the form input type="text" name="luggage_id" as a where clause in my model to update a row 'status' in 'consignment' table
My view
<form action='<?php echo site_url('clientaccount_ctrl/confirm_load_pickup'); ?>' method='post' name='process'>
<div class="form-group">
<input type="text" class="form-control" name="luggage_id" placeholder="Driver ID">
</div>
</div><!-- /.col -->
<div class="col-sm-4 invoice-col">
<button type="submit" class="btn bg-blue btn-flat margin">CONFIRM PICKUP</button>
</form>
My Controller
public function confirm_load_pickup(){
$this->load->model('Clientaccount_model');
$this->Clientaccount_model->confirm_load_pickup();
redirect('clientaccount_ctrl');
}
My Model
public function confirm_load_pickup(){
$luggage_id = $this->input->post('luggage_id');
$data['status']=3;
$this->db->where('luggage_id',$luggage_id);
$query=$this->db->update('consignment',$data);
return $query;
}
I am not an active code ignighter developer, but basic experience with several php mvc frameworks leads me to believe that the model doesn't extend a way to process form data as a controller would.
In the controller scope, $this refers to the controller.
In the model scope, $this refers to the model.
Assuming the model scope doesn't explicitly handle the controller scope and post / get methods, it would be unable to process the variable.
So, I would define the input in the controller and inject it into the model
Controller
public function confirm_load_pickup(){
$luggage_id = $this->input->post('luggage_id');
$this->load->model('Clientaccount_model');
$this->Clientaccount_model->confirm_load_pickup($luggage_id);
redirect('clientaccount_ctrl');
}
Model
public function confirm_load_pickup($luggage_id){
$data['status']=3;
$this->db->where('luggage_id',$luggage_id);
$query=$this->db->update('consignment',$data);
return $query;
}

Silex Framework, no route found for POST

I have a little problem with the silex framework (I'm quite sure, that it's caused by Silex)..
I have a form and want to submit it with POST, but Silex throws the following exceptions:
MethodNotAllowedException in UrlMatcher.php line 101:
MethodNotAllowedHttpException in RouterListener.php line 149:
No route found for "POST /checkPW": Method Not Allowed (Allow: GET)
Thats what my controller looks like:
$app->get('/checkPW', function () use ($app) {
return $app['templating']->render(
'checkPW_blog.php'
);
});
And that's what the form looks like:
<form method="post" action="/checkPW">
<div class="modal-body">
<div class="form-group">
<input type="password" class="form-control" id="password"
name="password"
placeholder="Passwort">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success" id="submitPW">
Passwort bestätigen
</button>
</div>
</form>
(realised with Bootstrap)
The bizarre thing is, that when I send the form with the method GET instead of POST everything works fine...
Does anybody know whats the problem here..?
Thanks all.
Look, you only define a route for get:
$app->get('/checkPW', function () use ($app) {
return $app['templating']->render(
'checkPW_blog.php'
);
});
Just define one for post:
$app->post('/checkPW', function () use ($app) {
// do post stuff...
});

Categories