How to start using Slug URLs without breaking old in Yii2 - url-rewriting

i want to rewrite my URLs from
/view?id=100
to
/view/100-article-title
But the site already has several thousand search pages. As far as I know, such a change can be bad for SEO. Is there a way to keep the old URLs working when switching to the new routing.
*I am creating links as follows:
Url::to(['view', 'id' => $item->id])
Is it possible to create links further through ID?

you can create getLink() function on your model and use it on. Then, when function runs you can check id if id <= 100 then return Url::to(['view', 'id' => $item->id]) else return Url::to(['view', 'id' => $item->id, 'slug' => $this->slug])
And add route with slug on your main.php

Look at my example.
First config urlManager in config.php in 'app/config' folder. In my case look like:
'components' => [
...
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
''=>'home/index',
'<slugm>-<id:\d+>-' => 'home/view',
],
],
.
...
],
and create link as fallow:
Url::to(['/home/view', 'id' => $model->home_id, 'slugm' =>$model->title_sr_latn])
and finaly url look like:
https://primer.izrada-sajta.rs/usluge-izrade-sajtova-1-

Related

Using spatie/laravel-analytics, how do I get page views for a specific pagePath?

I've just started looking at spatie/laravel-analytics, a package for getting data out of Google Analytics API. I'm a bit lost right now though as documentation seems thin... either that or I'm not searching for the right things.
What I want to do feels like it should be quite simple, get page views for each day over a period for a specific URL.
My current code block looks like this:
$analyticsData = Analytics::performQuery(
Period::days(7),
'ga:pageviews',
[
'metrics' => 'ga:pageviews',
'dimensions' => 'ga:pagePath',
'filters' => 'ga:pagePath==/pasta-recipes/lasagne/'
]
);
var_dump($analyticsData);
As always, any help appreciated.
In your case, just to replace this:
'dimensions' => 'ga:pagePath'
in:
'dimensions' => 'ga:date'
Output example:
use structure of function Analytics::fetchVisitorsAndPageViews(Period::days(7)); but use pagePath
$response = Analytics::performQuery(
Period::days(7),
'ga:users,ga:pageviews',
['dimensions' => 'ga:date,ga:pagePath']
);
$analyticsData = collect($response['rows'] ?? [])->map(function (array $dateRow) {
return [
'date' => Carbon::createFromFormat('Ymd', $dateRow[0]),
'url' => $dateRow[1],
'visitors' => (int) $dateRow[2],
'pageViews' => (int) $dateRow[3],
];
});
dd($analyticsData);

Laravel Voyager redirect to other page after login

I just started using Laravel and learning. I would like to redirect users to a custom section create through the BREAD.
In Voyager Controller, I see there is this function return Voyager::view('voyager::index');
I want to redirect to the page /admin/members
How can I achieve this? I tried to search around but can't find the solution. Hope someone can help with this. Thank you
I solved this problem by updating the user's configuration in config/voyager.php (Laravel config directory). Edit below configurations (redirect index)
'user' => [
'add_default_role_on_register' => true,
'default_role' => 'user',
'default_avatar' => 'users/default.png',
'redirect' => '/admin',
],
to
'user' => [
'add_default_role_on_register' => true,
'default_role' => 'user',
'default_avatar' => 'users/default.png',
'redirect' => '/admin/members',
],

Relations in resources?

So I'm planning to start using resources for my "API" (vue endpoint). So I started to search for some tutorials about the subject, and found a youtuber that describes the process. And I started making my own API resource. The youtuber shows briefly how to use the relations, but the thing is that I receive Property [description] does not exist on this collection instance. when trying to use the relation in the resource.
The current setup is:
$stack = Stack::select(['id', 'name', 'subject_id', 'description', 'image'])->where('id', '=', $requestId)->first();
$questions = $stack->load('question.choiceInRandomOrder');
return $questions;
And with resource it would be something like (notice choiceInRandomOrde, I would need that relation also):
return [
'subject' => $this->subject->name,
'name' => $this->name,
'slug' => $this->slug,
'description' => $this->description,
'image' => $this->image,
'questions' => [
'description' => $this->question->description,
'is_info' => $this->question->is_info,
'source' => $this->question->source,
'image' => $this->question->image,
]
];
}
And for testing, I have setup the following in my routes web.php
use App\Stack;
use App\Http\Resources\StackResource;
Route::get('/json', function(){
$stack = Stack::find(2);
return new StackResource($stack);
});
You try to access the name of subject in 'subject' => $this->subject->name, but you do not load the relation.
i don't know if i'm right but i thnk it has to be with the fact that ure not doing an Eloquent call but a Query Builder call (when doing $stack = Stack::select ... ). Why select just some fields in the call if you can choose the parameters to show directly in the model class? (see this).
Try to doing an Eloquent call instead (something like Stack::find(1)) and test it. It should work.

Change/Rename Controller Name In URL: Yii2

I was looking to change controller name in URL. Which, we can do by renaming the controller name in module. But, Through URL manager if we can do it. It will be better.
Module: user,
Controller: api,
Action: index
Right now,
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:(api)>/<action:\w+>/<id:[a-z0-9]+>' => 'user/<controller>/<action>',
'<controller:(api)>/<action>' => 'user/<controller>/<action>',
]
];
And, I can access it through.
http://dev.example.com/api/index
But, I was looking to change it to
http://dev.example.com/world/index
How can I do it? Any help/hint/suggestion is appreciable.
You can create custom url rules by adding items to the rules array.
So, in your case insert this into the rules array
'world/index' => 'api/index'
You can read more about URL rules here.
also you use ControllerMap
it useful when you are using third-party controllers and you do not have control over their class names.
below code in component in main.php in advance or web.php in basic
for example:
'controllerMap' => [
'api' => 'app\controllers\WorldController',
]

How to override needed messages from core messages in yii2

In yii2, i want to override some core messages translation, example:
in #yii/messages/vi/yii.php, has key => translated message:
'Update' => 'Sửa'
but in my application, i want to change this as:
'Update' => 'Cập nhật'
I have created the file: #app/messages/vi/yii.php, has only one the message which need to override:
return [
'Update' => 'Cập nhật'
];
in my main.php config, I added this to components:
'i18n' => [
'translations' => [
'yii' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#app/messages'
],
],
],
It works but just only for the override messages, others of core don't work.
I think you should copy yii.php from core to #common/messages/<your language>/yii.php and edit it. It should be work stable.
Try somehing like this
'i18n'=>[
'yii'=>[
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => "#vendor/yiisoft/yii2/messages",
'sourceLanguage' => 'en_US', // put your language here
'fileMap' => [
'yii'=>'yii.php',
]
]
]
],
Might be outdated thread but I'm putting this in case someone hits this in a search results as I did.
This works out for me :
// get the default translation file for desired language
$yii = require \Yii::getAlias('#vendor/yiisoft/yii2/messages/az/yii.php');
// just override what ever you want
return yii\helpers\ArrayHelper::merge( $yii,
[
'update' => 'myUpdateString',
// ... and so on
]);

Resources