Go to a page in a controller

-1

You see, I have a User table with a variable called role, for which I have this form:

@extends('layouts.app')
@section('content')

@Logged()
@include('partials.errors')
<form method="POST" action="../puesto">
    {{ csrf_field() }}
    <div class="form-group">
        <label for="rol" class="col-md-12 control-label"> <h2>{{ __("rol elegido") }}</h2>
        </label>
        <input name="rol" type="radio" value="administrador">   Administrador<br>
        <input checked="checked" name="rol" type="radio" value="cliente">   Cliente
    </div>
    <button type="submit" name="addrol" class="btn btn-default"> {{ __("Ingresar rol") }}
    </button>
</form>
@else
<h1 class="text-center text-mute" style="color:#FF0000"> {{ __("¡DEJA DE HACER EL INDIO Y INICIA SESIÓN!") }} </h1>
@endLogged
@endsection

Once the form is done, we go to this code in web.php:

Route::post('/puesto','UserController@acto');

Which brings me to the following:

public function acto(Request $request){
        $normas=[
            'rol' => 'required',
        ];

        $this->validate($request,$normas);

        Auth::user()->rol=$request->rol;
        Auth::user()->save();
        return back()->with('message', ['success', __("Rol seleccionado correctamente.")]);
    }

The return at the end will return me to the form, but I'm not interested in that, but I want you to take me to the main page ( Route :: get ('/', 'PlantsController @ index' );).

I have tried 2 back () but it gives an error. I have also tried putting 'return view (vegetal.index)' but it gives an error. How do I achieve it?

    
asked by Miguel Alparez 19.02.2018 в 09:42
source

2 answers

0

I have already fixed it. It was a matter of replacing the return-> back () with this code:

return redirect('/')->with('message', ['success', __("Rol seleccionado correctamente.")]);
    
answered by 19.02.2018 в 09:47
0

In Laravel it is considered a practical practice to use routes (url) in the code directly, for that there are the files of routes (web.php, api.php, etc ...).

The recommended thing is to point to the "name" of a route or even to the controller:

Instead of:

<form method="POST" action="../puesto">

It should be something like:

<form method="POST" action="{{ route('user.bla') }}">

And in routes \ web.php something like this is defined:

Route::post('puesto', 'UserController@acto')->name('user.bla');

As for the redirection, it would be something like this:

Route::get('/', 'PlantasController@index')->name('home');

and the redirect would be like this:

return redirect()->route('home')->with('message', ....);

What is the difference with what you use now?

That if for some reason the urls change, you do not have to do a search of these values for all the code of the application.

    
answered by 19.02.2018 в 13:57