Location of a variable

Asked

Viewed 39 times

-2

Route::get('/', function () {
    return view('welcome');
});
Route::view('/teste', 'teste');
Route::get('/noticia/{slug}', function($slug){
    if($slug != null):
        Route::redirect('noticia/{$slug}', '/');
    else:
        echo 'Erro de redirecionamento';
    endif;    
});
  • The function must redirect user access to the "Welcome" view whenever the value of $Slug is equal to empty ie: /news/(empty)

  • When I assign a value to the variable $Slug, I can fall to Else, and the script returns echo.

PROBLEM: Whenever I access /news returns to me error 404 not found.

QUESTION: Where is the value of the variable {$Slug stored}

1 answer

0

In this syntax, the Slug parameter is mandatory and since there is no route to /noticia the expected is actually 404 as return

'/noticia/{slug}'

To make it optional you need to use the following syntax

'/noticia/{slug?}'

This is described in the Laravel documentation here: https://laravel.com/docs/8.x/routing#Parameters-optional-Parameters

Browser other questions tagged

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