Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 控制器内有多个正则表达式,而不是使用单个正则表达式的多个路由_Regex_Laravel_Laravel 5_Routes - Fatal编程技术网

Regex 控制器内有多个正则表达式,而不是使用单个正则表达式的多个路由

Regex 控制器内有多个正则表达式,而不是使用单个正则表达式的多个路由,regex,laravel,laravel-5,routes,Regex,Laravel,Laravel 5,Routes,我运行Laravel 5.4 我有这些路线: <?php Route::get('/users/{name}', 'UsersController@showByText')->where('name', '[\w]+'); Route::get('/users/{id}', 'UsersController@showById')->where('id', '[\d]+'); ?> 和我的控制器: <?php namespace App\

我运行Laravel 5.4

我有这些路线:

<?php
    Route::get('/users/{name}', 'UsersController@showByText')->where('name', '[\w]+');
    Route::get('/users/{id}', 'UsersController@showById')->where('id', '[\d]+');
?>

和我的控制器:

<?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();
        }
    }
?>

问题:

我是否可以用一种方法来结束,以便能够做到:

<?php
    Route::get('/users/{mixed}', 'UsersController@show');
?>

我的控制器看起来像:

<?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();
            }
        }
    }
?>


我知道
Request::match()
不存在,并且知道
Route::pattern('id','[\d]+')将强制我的方法
显示
仅适用于数字。我想知道
Request
组件是否有
preg\u match()
的缩写。

如果(是数字($mixed))
并且您将
$mixed
作为参数传递给函数定义,那么
呢。我更喜欢Request或larvel助手,但让我们向这个伙伴,希望一些Laravel福音传道者来到这个主题并听到我的声音:)如果(是数字($mixed))
并且你将
$mixed
作为参数传递给你的函数定义,那么
怎么样呢?我更喜欢一个请求或Laravel助手,但让我们向这位伙伴致意,希望一些Laravel福音传道者来到这个主题并听到我的声音:)