How to recover information from Laravel URL?

Asked

Viewed 321 times

-2

I have the following route:

# Minha rota para cadastro de pessoas ao escolher um plano
Route::get('/cadastrar/{plano}', function($plano = 'silver'){
    # Checa se o plano existe
    if (array_search($plano, ['silver', 'gold', 'diamond']) === false)
        # Caso não exista, usa o "silver" como padrão 
        return redirect()->route('cadastrar', ['plano' => 'silver']);
    # Retorna a View para o usuário
    return view('auth.cadastrar');
# Condições de existência para o campo plano
})->where(['plano' => '[a-z]+'])->name('cadastrar');

But I need that within the View I can retrieve the name of the chosen plan, for example:

# http://exemplo.com/cadastrar/silver
# Parabéns você escolheu o plano {{$plano}}

And I’d like to know if the way I did this route is the best, or is there any more professional way, thank you!!

2 answers

0

Friend, see in the documentation Laravel: Passing Data To Views

Just pass an array in the second argument in the call from view.

Below is an example of how to implement within your code.

# Minha rota para cadastro de pessoas ao escolher um plano
Route::get('/cadastrar/{plano}', function($plano = 'silver'){
    # Checa se o plano existe
    if (array_search($plano, ['silver', 'gold', 'diamond']) === false)
        # Caso não exista, usa o "silver" como padrão 
        return redirect()->route('cadastrar', ['plano' => 'silver']);
    # Retorna a View para o usuário
    return view('auth.cadastrar', compact('plano'));
# Condições de existência para o campo plano
})->where(['plano' => '[a-z]+'])->name('cadastrar');

-1

I noticed some practices that are not very advisable, such as a check(if) inside the Laravel route file.

1° Step - Remove the logic of this file and move into a function in the model. Where your business rules should really be.

2° Step - To retrieve which plan was chosen you can either return a variable to the view or create a session variable Session('Chosen Plank') through the controller.

Browser other questions tagged

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