How can I define a route differently if parameter is not integer

后端 未结 4 1392
无人及你
无人及你 2021-01-17 08:08

I am using Laravel 5 and working on my local. I made a route with a parameter of {id} and another route with a specific name like so :

Route::get(\'contacts/         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-17 09:07

    Although the accepted answer is perfectly fine, usually a parameter is used more than once and thus you might want to use a DRY approach by defining a pattern in your boot function in the RouteServiceProvider.php file located under app/Providers (Laravel 5.3 and onwards):

     /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        Route::pattern('id', '[0-9]+');
    
        parent::boot();
    }
    

    This way, whereever you use your {id} parameter the constraints apply.

提交回复
热议问题