I'm trying to add an optional parameter in Laravel for group routes using prefix, but it doesn't work.
In the Laravel documentation I see:
Make sure to give the route's corresponding variable a default value
But how do I do it for prefixes? I see example only for a special route, but I need it for a group of routes.
I use Laravel 8.0.
There may or may not be space here, but it only works when there is space.
Route::middleware('api')
->prefix('v1/{space?}/')
->namespace($this->namespace)
->group(__DIR__ . '/../api.php');
Related
I am working on laravel7 Project, I want to change default Auth::route() route paths
I have login page and I want to change it to something like: /random/random2/random3/login
but laravel auth comes with "/login". how can I edit it?
If you just want to prefix them, you can wrap it in a route group:
Route::prefix('random/random2/random3/')->group(function(){
Auth::routes();
}).
I haven't tested this but it should work because Laravel references the routes by their name (e.g. route('login')) which would stay the same.
Alternatively you could just define your own routes instead of using Auth::routes() at all. You can copy the routes from the laravel/ui package and adjust them as needed.
I am trying to write a simple REST API backend that resides on
http://api.stan.test/.
Got laravel/framework:v7.21.0 and PHP:7.2.30.
Can't get the route matched.
The problem:
when I am accessing the http://api.stan.test/api/t.json URL
With api.stan.test string as domain group, my route in \Illuminate\Routing\RouteCollection::match is compared to api.stan.testapi/user alias and route is not found obviously.
With api.stan.test/ string as domain group, my route in \Illuminate\Routing\RouteCollection::match is compared against api.stan.test/api/user alias, match is found but it fails domain validation in \Illuminate\Routing\Route::matches.
preg_match($hostRegex, $request->getHost()) fails because
preg_match("{^api\.stan\.test/$}sDiu", "api.stan.test") are the values.
This is my RouteServiceProvider
# \App\Providers\RouteServiceProvider::mapApiRoutes
Route::domain('api.stan.test')
->prefix('/api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
and here is included routes file
# routes/api.php
Route::get('t.json','TController#api');
In Laravel, route parameters matching uses Symfony's Route compiler. Actually route compiler was finding dot (.) as a segment separator. This is the reason why in your case it cannot find the route.
You can do something like this :
Route::get('t_json','TController#api');
I am trying to create an API with Laravel 5.6, however, it seems to me that it's not possible to use optional route parameters before/after the parameter.
I'd like to achieve the following:
Route::get('api/lists/{id?}/items',
[
'as' => 'api/lists/items/get',
'uses' => 'ListsController#getListItems'
]);
With the above scenario, if I'm trying to visit api/lists/1/items it shows the page. On the other hand, if I'm trying to visit api/lists/items it says that the page is not found.
What I basically want is if there's no List ID specified, Laravel should fetch all the List ID's items, otherwise it should only fetch the specific ID's items.
Q: How is it possible to the optional parameter in between the 'route words'? Is it even possible? Or is there an alternative solution to this?
As far as I know, it's not possible to use optional parameters in the middle of your url.
You could try a workaround by allowing 0 for the optional parameter and loading all items in this case, as described here.
However, I'd recommend going with two different routes here to match everything you want:
api/lists/items
api/lists/{id?}/items
You have to give default value for optional parameter in the controller:
Route
Route::get('api/lists/{id?}/items', 'ListsController#getListItems');
ListsController
public function getListItems($id = null) {
---your code----
}
Reference
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?
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))$');
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!
I'm working with the following routing example:
https://github.com/phalcon/mvc/blob/master/simple-subcontrollers/app/config/routes.php
Can anyone explain to me why on line 12 there is this:
$router->add('/:controller', array(...
Haven't we already defined the route on line 5 as:
$router->add('/:controller/:action/:params', array(
"/:controller" is a subset of '/:controller/:action/:params'.
If you remove "/:controller" route, URLs ending with controller name will not be matched.
Why isn't router falling back to default action name that I can indicate as:
$router->setDefaultAction('index');
Is this a bug or a feature?
Thanks!
This route
$router->add('/:controller/:action/:params', array(
Will only execute if it gets all parts to make the route valid for example
myapp/users/logout/dave
The above will match what that route is looking for and cause the route to execute.
myapp/users
The above however wouldn't match due to missing actions and params also a url with a / and without aren't the same url.
Since you can add many routes as you need using add(), the order in which routes are added indicate their relevance, lastest routes added have more relevance than first added. Internally, all defined routes are traversed in reverse order until Phalcon\Mvc\Router finds the one that matches the given URI and processes it, while ignoring the rest.
Hope this helps.