I run Laravel 5.4.
I have those routes :
<?php
Route::get('/users/{name}', 'UsersController@showByText')->where('name', '[\w]+');
Route::get('/users/{id}', 'UsersController@showById')->where('id', '[\d]+');
?>
And my controllers :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
function showByText($name)
{
return DB::table('users')->where('name', 'LIKE', $name)->get();
}
function showById($id)
{
return DB::table('users')->where('id', $id)->get();
}
}
?>
Question :
Could I end up with one single method, to be able to do :
<?php
Route::get('/users/{mixed}', 'UsersController@show');
?>
And my controller would look like :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
function show()
{
if( Request::match('mixed', '[\w]+') )
{
return DB::table('users')->where('name', 'LIKE', $mixed)->get();
}
else if( Request::match('mixed', '[\d]+') )
{
return DB::table('users')->where('id', $mixed)->get();
}
}
}
?>
I know Request::match()
does not exists, and knowing that Route::pattern('id', '[\d]+');
will force my method show
to be compliant only for digits. I would like to know if there is a shorthand for preg_match()
with the Request
component.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire