Can I add a parameter to the Route :: group in Laravel, but delete it before sending to the route in Laravel?

I use Laravel 4 to create API names labeled accountname for each of my clients. Each client has its own identical database. Therefore, Foocorp should make api calls that look like this:

http://api.example.com/Foocorp/users/5

The call to Barcorp api is as follows

http://api.example.com/Barcorp/users/5

I have to provide the account name in the URL for business / branding reasons, so I cannot remove this parameter from the URL routes.

Here is the filter that I used to try to get the account name out of the route, check its activity and point to my database. I was hoping to remove the accountname parameter so that I could write all my controller functions so as not to include the $accountname parameter for all of them.

 Route::filter('accountverification', function() { $route = Route::getCurrentRoute(); $params = $route->getParameters(); $accountName = $params['accountname']; // verify account with this name exists and set up DB connection to point to their database // ... unset($params['accountname']); $route->setParameters($params); }); 

Here is my route group that uses a filter:

 Route::group(array('prefix' => '{accountname}', 'before' => 'accountverification'), function() { Route::get('users/{id}', ' UsersController@getShow ') ->where(array('id' => '[0-9]+')); }); 

The problem is that deleting a parameter in the filter has no effect when calling the controller / function. In the UsersController :: getShow function, the first parameter is always accountname from the group prefix.

Is there a way to include a variable / parameter in all my routes so that I can do something before sending a request that will not be passed to the function?

+4
source share
2 answers

This is the wrong way to use filters. In fact, if you define the filter "account name", then this is the name of the filter, but you use "account information".

What you should do is check the account name in the UserController constructor. Your route prefix must be a known value.

0
source

Yes, you can. Use the route function: forgetParameter($parameter) to remove the parameter from the controllers in the arguments. This feature is available in laravel 4.1 or higher. For your example:

 Route::filter('accountverification', function(Route $route) { $params = $route->getParameters(); $accountName = $params['accountname']; // verify account with this name exists and set up DB connection to point to their database // ... $route->forgetParameter('accountname'); }); 

For example, I use this to forget the locale parameter in my routes, so it is not included as an argument for each controller method within the route group.

http://laravel.com/api/4.2/Illuminate/Routing/Route.html#method_forgetParameter

Please leave a comment in the future if this link is broken, as I will update when necessary.

EDIT

In Laravel 5, you can also use middleware, as route filters are depreciated.

+4
source

Source: https://habr.com/ru/post/1488574/


All Articles