Route and form in Laravel

Asked

Viewed 719 times

2

I have a route

Route::get('/','WebController@index');

I have a controller

public function index()
{
    $cars = Cars::table('cars')->get();
    return view('web::index')->with('cars', $cars);
}

And in the form reads as follows URL

{!! Form::open(['class' => 'form-horizontal search-side-box', 'url' => '/', 'method' => 'post', 'autocomplete' => 'off']) !!}

But when I click the button submit of form, of the following error:

Class App\Front\Http\Controllers\WebController does not exist

But the controller is configured, so much so that in other places I call the URL '/' and it works, only on form that no. And when I pass another URL in the form it also works:

{!! Form::open(['class' => 'form-horizontal search-side-box', 'url' => '/about', 'method' => 'post', 'autocomplete' => 'off']) !!}
  • What your form does or should do?

  • In this case you pass the search data of a specific item. If you do not have any field filled it must fetch all the items, otherwise search by passing the form parameters

  • 1

    @Edinhorodrigues, you have to create a post route: Route::post('/','Webcontroller@index');

  • Thanks, I decided even better: Route::any('/', 'Webcontroller@index');

  • @Edinhorodrigues, before using Route::any() checks the security issue. I have to confirm, but if no mistake favors vulnerabilities.

1 answer

2


Well first your method in Form and in the routes is wrong, in one is defined a post and in the other the get. Normally get is used for simple pages, and the post in Formulars and so on, in your case I think there’s a bit of code missing there:

Set one more route

Route::get('/', 'WebController@index'); // renderiza a pagina com o form
Route::post('/busca_detalhe', ['as'=>'car.busca_detalhe', 'uses'=> 'WebController@busca_detalhe']); // Retornará os resultados submetidos na busca do form

Add a function in the Webcontroller

public function busca_detalhe(Request $request)
{
    $dados = $request->except('_token');
    $cars = DB::table('cars')->select('*')->where('parametro', '=', $dado['parametro'])->->get();
    return view('car.detalhes', compact('cars')); //Crie a view para mostrar os resultados
}

You can also set the Form opening with the route instead of the url:

{!!  Form::open(['route' => 'car.busca_detalhe', 'method' => 'POST', 'class'=>'form-horizontal bordered-row']) !!}  

Don’t try to do everything in the same/Controller function using the same routes, it will confuse you at the time of maintenance, and I don’t need to mention if another developer is going to analyze your code one day...

  • Thanks Darlei, I made the route passes any, so can I do both get Qt post, q think, can it be like this? EX: Route::any('/', 'Webcontroller@index');

  • @Edinhorodrigues, before using Route::any() checks the security issue. I have to confirm, but if no mistake favors vulnerabilities.

  • Laravel is very consistent in terms of security, but really, any loophole that can be avoided is best avoided...it doesn’t cost to create a new route and view

  • 1

    @Darleifernandozillmer, true! Laravel is very robust in terms of safety! The problem is the inappropriate use of the implement culture to function and embed vulnerability with it!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.