2

In my Laravel routes/web.php file I have defined the following two routes:

Route::get('transaction/{id}', ['uses' => 'PaynlTransactionController@show'])->name('transaction.show');
Route::get('transaction/{txId}', ['uses' => 'PaynlTransactionController@showByTxId'])->name('transaction.showByTxId');

In my RouteServicesProvider I have defined the following two patterns:

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))$/');

Whenever I go to transaction/<id> the routing works correctly, as long as id is an integer. However, when I go to transaction/TX874-152268, for example, it doesn't match any route and I receive the NotFoundHttpException in RouteCollection.php error.

I've validated the txId regex and it gives a full match: https://regex101.com/r/kDZR4L/1

My question: how come only my id pattern is working correctly, whereas my txId pattern isn't?

1
  • @WiktorStribiżew thanks, I've tried but no luck. It looks a lot nicer tho :-) Commented Dec 21, 2016 at 14:13

2 Answers 2

1

In the route

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))$/'); 

I had included the forward slashes at the start and end of the string. This should not be included when passing a pattern to Route::pattern. Thus the following works:

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))$'); 
Sign up to request clarification or add additional context in comments.

Comments

1

Because the urls are both /transaction/{value} it will get the last on.

If you change /transaction/{txId} to /transaction/tx/{txId} it will be clear for the routes.

Routes can only get one, so when you assign the prefix (at this time /transaction) to both of the urls it doesn't work.

You can also use /transaction/TX{txId}, in your controller you can past TX before the txId variable.

public function showByTxId($txId) {
    $txid = "TX".$txid;
}

Edit:

Remove the / add the start. 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))$');

Hope this works!

5 Comments

That shouldn't matter, right? I'm doing the exact same thing for users/{id} and users/{username} with the regex [0-9]+ and [A-Za-z]+, respectively, and this works.
I agree, but lets try it. If its works its nice ;) @pbond Btw, pay.nl is een goede betalingsprovider ;)
Tried your suggestion, but no luck: I've adjusted the regex to not match the TX part anymore, and prepended TX in web.php to {txId}, but this doesn't work either, unfortunately.
Same as before, NotFoundHttpException in RouteCollection.php line 161:. I could add the stack trace in the original question if that helps.
Already got it, I shouldn't have included the / at the start and end of the pattern, apparently.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.