How can I define a route differently if parameter is not integer
Just add ->where('id', '[0-9]+')
to route where you want to accept number-only parameter:
Route::get('contacts/{id}', 'ContactController@get_contact')->where('id', '[0-9]+');
Route::get('contacts/new', 'ContactController@new_contact');
Read more: http://laravel.com/docs/master/routing#route-parameters
A simple solution would be to use an explicit approach.
Route::get('contacts/{id:[0-9]+}', 'ContactController@get_contact');
Route::get('contacts/new', 'ContactController@new_contact');
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.