laravel5 Route::resource Generate wrong route - laravel

I am using Laravel 5.7 Route::resource.
I has saw
https://laravel.com/docs/5.7/controllers
when I use
Route::resource('koumokus', 'KoumokuController');
should be Generate as bleow
GET admin/koumokus/{koumoku} show
GET admin/koumokus/{koumoku}/edit edit
However, It is Generate
GET admin/koumokus/{koumokus} show
GET admin/koumokus/{koumokus}/edit edit
on the other hand,
Route::resource('funruis', 'FunruiController');
is normally to gent
GET admin/funruis/{funrui} show
GET admin/funruis/{funrui}/edit edit
How can I fixed it?

Sometimes laravel fails to find the conventional name for the parameter on the resource routes. It has happened to me sometimes when I used words that are not in english. But it allows you to customize that name. You can take a look at the docs here ->
Naming Resource Route Parameters
Since you are working with the 'koumokus' resource here, you can specify that the parameter for the 'koumokus' resource must be 'koumoku'.
Route::resource('koumokus', 'KoumokuController', [
'parameters' => [
'koumokus' => 'koumoku'
]
]);

Related

Is there a one time link generation Laravel?

Is it possible to create a one time link in Laravel? Once you open the link it expires?
I have created a Temporary Signed Link, but I can open it multiple times. How do I counter it?
There is this package that can help you
https://github.com/linkeys-app/signed-url/
This will generate a link valid for 24hours and for just one click .
$link = \Linkeys\UrlSigner\Facade\UrlSigner::generate('https://www.example.com/invitation', ['foo' => 'bar'], '+24 hours', 1);
The first time the link is clicked, the route will work like normal. The second time, since the link only has a single click, an exception will be thrown. Of course, passing null instead of '+24 hours' to the expiry parameter will create links of an indefinite lifetime.
There maybe a package that provides a functionality like this... always worth looking on Packagelist before building something rather generic like this from scratch. But, it's also not a hard one to build from scratch.
First you'll need database persistence, so create a model and a migration called UniqueLink. In the migration you should include a string field called "slug", a string field called path, and a timestamp field called "used_at."
Next create a controller with a single __invoke(string $slug) method. In the method look up the $link = UniqueLink::where('slug', $slug)->first(); Update the models' used_at parameter like so $link->update(['used_at' => Carbon::now()]);
Then return a redirect()->to($link->path);
Add a route to your routes file like this Route::get('/unique-link/{slug}', UniqueLinkController::class);
Now you'll just need to create a method to add these links to the db which create a slug (you could use a UUID from Str::uuid() or come up with something more custom) and a path that the link should take someone. Over all a pretty straight forward functionality.
You could track when the URL is visited at least once and mark it as such for the user if you really want to, or you could reduce the expiry down to a few mins.
URL::temporarySignedRoute( 'foobar', now()->addMinutes(2), ['user' => 100] );

Laravel 5 - Define custom route method?

I just got an issue , i have 2 problems :
I want create a custom route for fast using without copy past code many time. Example Laravel 5 have default Route:resource (...) to make Restful! But i want to make my custom route function , Route:api(...) , Route:xxx(...) ... and I can custom it what I want !
How can I use multi route file ? Example : I can define route in App\User\route.user.php , App\Book\route.book.php .... because now, I can only use route file in route folder default !
I do not understand properly question 1. But for question 2, try this:
Go to app/Providers/RouteServiceProvider.php. Look for the function mapWebRoutes(). The line
require base_path('routes/web.php');
Duplicate it and change so you now have :
require base_path('routes/web.php');
require base_path('app/User/route.user.php');
require base_path('app/Whatever/route.whatever.php');
And laravel will load all routes within those files. Now, I've tested this, it works (Laravel 5.3) but I can't guarantee anything or if there are going to be conflicts with routes (duplicates). But yeah, it works.

Bitrix CMS, how to get cached data based on GET parameter in template of standart component?

I'm working with a component bitrix:catalog (which is standard one) and faced an issue. I want to add some extra GET parameters to switch view mode. I think there is no need to rewrite whole component to make such switcher, so I added extra keys in result_modifier in a way similar to:
$this->__component->arResultCacheKeys = array_merge($this->__component->arResultCacheKeys, array('key1', "key2"));
Earlier in the same result_modifier I perform adding those extra keys in $arResult['key1'] etc. They seem to be correctly saved, but only for current inquiry such as ?view=list or view=card, that means only one variable value is saved and it does not react on changing of GET parameter. Is there simple and correct way to make that component to cache and to output data based on GET variable? The only idea which came to my mind is to rewrite component by adding extra parameter and checking of GET, but I think there must more simple and correct solution to make in via template. Human Readable Links are turned on. And I want to have auto-cash being turned on as well. If I turn it off it starts working as planned.
One of possible solutions is to rewrite it cache by SetTemplateCachedData but it still seems to me rough and incorrect way for such simple task.
Bitrix masters please help me to find correct solution, google can't help at the moment.
If you use standard bitrix:catalog component, you may be use standart bitrix:catalog.section. In that component.php used standart component cache.
That means you can describe additional parametr in you custom .parameters.php, and set it in bitrix:catalog.section params.
Standart component cache set cacheId based on arParams.
So you include component should look like this:
$APPLICATION->IncludeComponent(
"bitrix:catalog.section",
"",
array(
"IBLOCK_TYPE" => $arParams["IBLOCK_TYPE"],
"IBLOCK_ID" => $arParams["IBLOCK_ID"],
"ELEMENT_SORT_FIELD" => $arParams["ELEMENT_SORT_FIELD"],
"ELEMENT_SORT_ORDER" => $arParams["ELEMENT_SORT_ORDER"],
....
....
"NEW_ADDITIONAL_GET_PARAMS"=> $_GET['view']
),
$component
);
Of course better way somethink like
"NEW_ADDITIONAL_GET_PARAMS"=> (in_array($_GET['view'],array('list','card'))?$_GET['view']:'list')
But may be you need just set right catalog params: SEF_MODE SEF_FOLDER SEF_URL_TEMPLATES

Route is not defined after updating laravel 5.1.8 to 5.1.24

I updated my laravel installation with the composer update and i get this error.
Route [categoryid] not defined
The strange thing is that, before the update, it worked just fine.
My routes.php file looks like this:
Route::group(['prefix'=>'category'], function () {
Route::get('mobilephones', [
'as'=>'mobilephones',
'uses'=>'PhoneController#getShow'
]);
Route::get('{categoryid}', [
'as'=>'categoryid',
'uses'=>'CategoryController#one'
]);
Route::get('{categoryid}', [
'as'=>'computerscategoryid',
'uses'=>'CategoryController#one'
]);
});
and i am calling the route with this html code
<li>Argument</li>
Everything used to work so is anyone aware of a change in the Group route files after 5.1.8?
As a general rule, always run php artisan route:list to see the compiled list of your routes.
You have two routes that do the exact same thing:
Route::get('{categoryid}', [
'as'=>'categoryid',
'uses'=>'CategoryController#one'
]);
Route::get('{categoryid}', [
'as'=>'computerscategoryid',
'uses'=>'CategoryController#one'
]);
They accept the same argument. They get sent to the same controller action. The only difference is that they have different route names. One of them (the second one) is most likely overriding the other. I would suggest removing the second one - computerscategoryid - because I can't see a purpose in having both of them.

Laravel - accessing lang/en/validation.php from test suite

I want to test that warning messages are displayed correctly when form fields are returned as invalid. The best way to do this would be to access the array in validation.php to get the name of the language line that I want to use.
How can I do this?
The documentation for Laravel is very great. You can access language files in resources/lang/{locale}/ easily with the provided trans() function (Laravel 5):
$expected = 'The :attribute may only contain letters.';
$actual = trans('validation.alpha');
$this->assertEquals($expected,$actual);
Please provide your attempts in the question next time. Also have a look at the SO tour (you will be rewarded with a bronze badge afterwards!)

Resources