Laravel resource route parameters not overriding - laravel

I am building a Laravel 8 api. I'm trying to set up a show route for /api/access-type/{access_type}. I want to change the {access_type} to be {id}
This is the route definition:
Route::middleware(['auth:api'])->group(function () {
/////////// not allowed to use these. Creating/deleting these resources would require code changes
Route::apiResource(
\App\Http\Controllers\AccessTypeController::$uri,
'AccessTypeController'
)->only(['index', 'show'])->parameters(['access_type' => 'id']);
});
When I look at the listed routes I can see that the route hasn't changed:
GET|HEAD | api/access-types/{access_type} | access-types.show | App\Http\Controllers\AccessTypeController#show | Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful | Illuminate\Routing\Middleware\ThrottleRequests:api | Illuminate\Routing\Middleware\SubstituteBindings | App\Http\Middleware\Authenticate:api
If I use postman to visit /api/access-types/3 I can see that there is no $request->id variable, but there is a $request->access_type variable, so the overwrite isn't working. What am I doing wrong? This should work according to: docs

I fixed it with the following:
Route::apiResource(
\App\Http\Controllers\AccessTypeController::$uri,
'AccessTypeController'
)->parameters(['access-types' => 'id']);
The parameter had to match the uri that I was setting it up for. I suppose this is so you can group multiple different resource routes into one.

Related

react-i18next cannot resolve key

I have a problem with react-i18next not resolving my keys, so everything I get as an output are the keys themself.
I had to embed my project as gui project to a VisualStudio solution. Running my original project works just fine, the solution project in contrast cannot resolve the key part of my translate function call.
t('user:KEY_CONSTANT') //output: KEY_CONSTANT
My i18n.config looks like this:
i18n
.use(XHR)
.init({
lng: i18nHelper.languageDetector(),
load: 'currentOnly',
fallbackLng: 'en-US',
backend: {
loadPath: 'i18n/{{lng}}/{{ns}}.json'
},
ns: ['admin', 'user'],
defaultNS: 'admin',
debug: false,
interpolation: {
escapeValue: false,
formatSeparator: ',',
format: function (vale, format, lng) {
if (format === 'uppercase') return value.toUpperCase();
return value;
}
}
});
When embedding the project in the solution, I had to change webpacks output folder, which I think is the actual reason of malfunctioning, but I can't find where exact the problem occurs. I tried changing the loadPath, but if thats the source of failure, I just didn't try it the right way :S
My project tree looks like:
Solution
|-gui
| |-src
| |-i18n
| |-de-DE(containing the german admin.json and user.json files)
| |-en-US(containing the english admin.json and user.json files)
| |-utils
| |-i18n.js
|-out
| |-prgFiles
| |-html
| |-i18n(contains same items as i18n under src)
In my old project, the webpack output was '/dist/' on the same level as src, in the solution, '../out/prgFiles/html'.
admin.json right now contains no keys at all, user.json contains the keys just like you'd expect:
{
"KEY_CONSTANT": "Actual string value"
}
As said, I tried around a bit with path changes, checked for translate, i18next and i18next-Provider beeing found and everythings fine. It just cant resolve KEY_CONSTANT to the actual value.
Do you have an idea what the problem might be?
set debug option to true in i18next init -> i bet you will get an error stating something like backendConnector failed to load namespace user...
make sure the clientside is able to load the translations (the webpack-dev server does a good job in doing so - but in production you will need to serve those file yourself)

SpecFlow Step Generation for Scenario Outline Generating Incorrect Methods

I'm new in Visual Studio. I'm using Visual Studio 2015 with SpecFlow. Below is the Feature File:
#mytag
Scenario Outline: Successful Authentication
Given I am a user of type <user>
When I hit the application URL
And I provide <email>
And I click on Log In Button
Then I will be landed to the Home Page
And I will be able to see <name> on the Home Page
Examples:
| user | email | name |
| admin | a.b#example.com | alpha |
| non-admin | b.c#example.com | beta |
When I generate the step definitions I'm expecting parameters in place of the variables, instead the method is generated as below:
[Given(#"I am a user of type admin")]
public void GivenIAmAUserOfTypeAdmin()
{
ScenarioContext.Current.Pending();
}
I was instead expecting a method like:
[Given(#"I am a user of type '(.*)'")]
public void GivenIAmAUserOfType(string p0)
{
ScenarioContext.Current.Pending();
}
What am I missing?
As an example, surrounding the <user> in the Given step with '' like this,
Given I am a user of type '<user>'
will generate the desired results. It's probably needed in order to recognize the regular expression.

How do you phpunit test redirection to urls with extra (dynamic) parameters?

In L4.2, I have been trying to use the phpunit helpers assertRedirectedToRoute() or assertRedirectedToAction(), but they don’t work when there are additional optional params put on the url.
For instance, I have this route:
Route::get('properties/create/{step?}/{property_id?}', [
'as' => 'admin.properties.createStep',
'uses' => 'PropertiesController#createStep']);
However, $this->assertRedirectedToAction(‘App\Controllers\PropertiesController#createStep’) fails with the following response:
1) AdminPropertiesTest::testSearchPostDataWithoutEANPropertyId
Failed asserting that two strings are equal.
--- Expected
+++ Actual
## ##
-'http://localhost/admin/properties/create'
+'http://localhost/admin/properties/create/3/194921'
How do I get an assertion to pass without declaring the "3" or "194921" in the url (as they are dynamic) and using assertRedirectedTo()?

Laravel 5.0, env() returns null during concurrent requests

The problem is that when I try to get a config variable using env('setting') or \Config::get('setting'), sometimes it returns null.
For the testing reason I created a simple route:
Route::get('/test', function () {
$env = env('SETTING');
if (!$env) {
\Log::warning('test', [$env]);
}
});
Then I used apache benchmark. And the results were like this:
Calling only one request at a time (ab -n 100 -c 1 http://localhost/test) there were no problem, no records in the log file
Calling with 10 concurrent requests (ab -n 100 -c 10 http://localhost/test) I got about 20 lines like this: [2015-06-22 14:19:48] local.WARNING: test [null]
Does anybody know, what can be the problem? Is there something missing in my configuration or in php settings?
This is a know bug in dotenv package - see the discussion here
https://github.com/laravel/framework/issues/8191
This happen to me as well. My workaround is in your config/app.php you have to add this:
'setting' => env('SETTING'),
Then when you want to get the setting config you have to do this:
$env = config('app.setting');

Prefixing routes with locale in Symfony2

Here is my setup, a bit changed standard edition.
# app/config/routing.yml
_internal:
resource: "#AcmeDemoBundle/Controller/"
prefix: /{_locale}
requirements:
_locale: en|sr
And added /{_locale} in security.yml.
Routes before:
Name Method Pattern
_welcome ANY /
_demo_login ANY /demo/secured/login
_security_check ANY /demo/secured/login_check
_demo_logout ANY /demo/secured/logout
acme_demo_secured_hello ANY /demo/secured/hello
_demo_secured_hello ANY /demo/secured/hello/{name}
_demo_secured_hello_admin ANY /demo/secured/hello/admin/{name}
_demo ANY /demo/
_demo_hello ANY /demo/hello/{name}
_demo_contact ANY /demo/contact
_wdt ANY /_wdt/{token}
_profiler_search ANY /_profiler/search
_profiler_purge ANY /_profiler/purge
_profiler_import ANY /_profiler/import
_profiler_export ANY /_profiler/export/{token}.txt
_profiler_search_results ANY /_profiler/{token}/search/results
_profiler ANY /_profiler/{token}
_configurator_home ANY /_configurator/
_configurator_step ANY /_configurator/step/{index}
_configurator_final ANY /_configurator/final
Routes after:
Name Method Pattern
_welcome ANY /
_wdt ANY /_wdt/{token}
_profiler_search ANY /_profiler/search
_profiler_purge ANY /_profiler/purge
_profiler_import ANY /_profiler/import
_profiler_export ANY /_profiler/export/{token}.txt
_profiler_search_results ANY /_profiler/{token}/search/results
_profiler ANY /_profiler/{token}
_configurator_home ANY /_configurator/
_configurator_step ANY /_configurator/step/{index}
_configurator_final ANY /_configurator/final
_demo_login ANY /{_locale}/demo/secured/login
_security_check ANY /{_locale}/demo/secured/login_check
_demo_logout ANY /{_locale}/demo/secured/logout
acme_demo_secured_hello ANY /{_locale}/demo/secured/hello
_demo_secured_hello ANY /{_locale}/demo/secured/hello/{name}
_demo_secured_hello_admin ANY /{_locale}/demo/secured/hello/admin/{name}
_demo ANY /{_locale}/
_demo_hello ANY /{_locale}/hello/{name}
_demo_contact ANY /{_locale}/contact
Which ofc leads to No route found for "GET /en/demo/hello/World" for /en/demo/hello/World url.
Any idea how to fix this?
Additionally, I would like to implement per domain locale. I was trying https://github.com/schmittjoh/JMSI18nRoutingBundle, but for some reason it does not work with latest Symfony 2.0.9, at least for me. I was following the doc, but I got similar 404 error when trying prefixed urls.
Defining default values and requirements for placeholders in prefix is not possible in Symfony version 2.0.9. You'd have to upgrade to 2.1, which is currently in active development.

Resources