Laravel 路由不匹配模式
Laravel route not matching pattern
在我的 Laravel routes/web.php
文件中,我定义了以下两条路线:
Route::get('transaction/{id}', ['uses' => 'PaynlTransactionController@show'])->name('transaction.show');
Route::get('transaction/{txId}', ['uses' => 'PaynlTransactionController@showByTxId'])->name('transaction.showByTxId');
在我的 RouteServicesProvider
中,我定义了以下两种模式:
Route::pattern('id', '[0-9]+');
Route::pattern('txId', '/^(TX(1[0-9]\d|[2-9]\d\d)-(1[0-9]\d\d\d\d|[2-9]\d\d\d\d\d))$/');
每当我去 transaction/<id>
时,只要 id
是一个整数,路由就会正常工作。但是,例如,当我转到 transaction/TX874-152268
时,它不匹配任何路由,我收到 NotFoundHttpException in RouteCollection.php
错误。
我已经验证了 txId 正则表达式,它给出了完全匹配:https://regex101.com/r/kDZR4L/1
我的问题:为什么只有我的 id
模式可以正常工作,而我的 txId
模式却不能?
因为 url 都是 /transaction/{value}
它将在最后。
如果您将 /transaction/{txId}
更改为 /transaction/tx/{txId}
,那么路线会很清楚。
路由只能获取一个,因此当您将前缀(此时 /transaction
)分配给两个 url 时,它不起作用。
您也可以使用 /transaction/TX{txId}
,在您的控制器中,您可以在 txId
变量之前传递 TX
。
public function showByTxId($txId) {
$txid = "TX".$txid;
}
编辑:
删除/
添加开始。
Route::pattern('txId', '^(TX(1[0-9]\d|[2-9]\d\d)-(1[0-9]\d\d\d\d|[2-9]\d\d\d\d\d))$');
希望这有效!
在路线中
Route::pattern('txId', '/^(TX(1[0-9]\d|[2-9]\d\d)-(1[0-9]\d\d\d\d|[2-9]\d\d\d\d\d))$/');
我在字符串的开头和结尾加入了正斜杠。将模式传递给 Route::pattern
时不应包含此内容。因此以下作品:
Route::pattern('txId', '^(TX(1[0-9]\d|[2-9]\d\d)-(1[0-9]\d\d\d\d|[2-9]\d\d\d\d\d))$');
在我的 Laravel routes/web.php
文件中,我定义了以下两条路线:
Route::get('transaction/{id}', ['uses' => 'PaynlTransactionController@show'])->name('transaction.show');
Route::get('transaction/{txId}', ['uses' => 'PaynlTransactionController@showByTxId'])->name('transaction.showByTxId');
在我的 RouteServicesProvider
中,我定义了以下两种模式:
Route::pattern('id', '[0-9]+');
Route::pattern('txId', '/^(TX(1[0-9]\d|[2-9]\d\d)-(1[0-9]\d\d\d\d|[2-9]\d\d\d\d\d))$/');
每当我去 transaction/<id>
时,只要 id
是一个整数,路由就会正常工作。但是,例如,当我转到 transaction/TX874-152268
时,它不匹配任何路由,我收到 NotFoundHttpException in RouteCollection.php
错误。
我已经验证了 txId 正则表达式,它给出了完全匹配:https://regex101.com/r/kDZR4L/1
我的问题:为什么只有我的 id
模式可以正常工作,而我的 txId
模式却不能?
因为 url 都是 /transaction/{value}
它将在最后。
如果您将 /transaction/{txId}
更改为 /transaction/tx/{txId}
,那么路线会很清楚。
路由只能获取一个,因此当您将前缀(此时 /transaction
)分配给两个 url 时,它不起作用。
您也可以使用 /transaction/TX{txId}
,在您的控制器中,您可以在 txId
变量之前传递 TX
。
public function showByTxId($txId) {
$txid = "TX".$txid;
}
编辑:
删除/
添加开始。
Route::pattern('txId', '^(TX(1[0-9]\d|[2-9]\d\d)-(1[0-9]\d\d\d\d|[2-9]\d\d\d\d\d))$');
希望这有效!
在路线中
Route::pattern('txId', '/^(TX(1[0-9]\d|[2-9]\d\d)-(1[0-9]\d\d\d\d|[2-9]\d\d\d\d\d))$/');
我在字符串的开头和结尾加入了正斜杠。将模式传递给 Route::pattern
时不应包含此内容。因此以下作品:
Route::pattern('txId', '^(TX(1[0-9]\d|[2-9]\d\d)-(1[0-9]\d\d\d\d|[2-9]\d\d\d\d\d))$');