jeudi 27 avril 2017

Understand Laravel route with optional parameter

I'm trying to create a route with one optional parameter with Laravel 5.4.

Route :

Route::get('/run/{zone?}/id/{id}', 'RunController@show');

Controller :

class RunController extends Controller
{
    public function show()
    {
        $route = \Route::current();
        $zone = $route->parameter('zone') ?: 'default';
        $id = $route->parameter('id');

        Run::setZone($zone);
        $run = Run::findOrFail($id);
        return view('run.show')->with(['run' => $run]);
    }
}

The url run/test/id/42 works like expected.

But with run/id/42 I got a nice NotFoundHttpException in RouteCollection.php when I expect the same result than run/default/id/42

What did I missed ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire