Yii2 Two different way for breadcrumbs - yii2

I have a gii generated view.php page, which can be reached from two different paths, but the breadcrumbs is the same. Anyone know how to fix?
Let me explain: I have the view.php view which shows the summary of the report made and can be reached from two paths:
when I create a new report and therefore the path should be HOME / CREATION / VIEW;
and also from a section that shows the user the summary of the reports she sent and therefore the breadcrumbs should be HOME / SUMMARY / VIEW.

You have 2 options to know where it came from:
Send a query variable on each link or in the redirection to manually build the breadcumb. Like: $value = Yii::$app->request->getQueryParam('breadcumb')
Other option is to get the referrer url. And base on the value you can pass it to the switch. You can get referrer's url using: $value = Yii::$app->request->getReferrer().
Then do a switch to build the link:
switch(value) {
case 'creation':
$label = 'CREATION';
$url = 'url_of_creation';
break;
case 'summary':
$label = 'SUMMARY';
$url = 'url_of_summary';
break;
}
Then just do something like this:
$this->params['breadcrumbs'][] = ['label' => $label, 'url' => $url];

This is a short breadcrum solution using match():
$this->params['breadcrumbs'][] = match($path) {
'creation' => ['label' => 'Creation', 'url' => Url::to['creation'],
'summary' => ['label' => 'Summary', 'url' => Url::to['summary'],
};
$path should either be set in the controller or determined by Yii::$app->request->getReferrer().
Please note, that this requires PHP8.

Related

Why not returning full value on Laravel -Guzzle

I am trying to return the JSON data from a third party API with Laravel-Guzzle. Everything is working but the value is different it's like some kind missing characters specially on images.
Here's Laravel-Guzzle code :
$client = new \GuzzleHttp\Client();
$request = $client->request('GET',
'https://targetweb.com/api/v2/search_items/?', [
'query' => [
'by' => 'relevancy',
'keyword' => 'productx',
'limit' => 50,
'newest' => 0,
'order' => 'desc',
'page_type' => 'search',
'version' => 2
]
]);
$array = json_decode($request->getBody()->getContents(), true);
$nArrItems = count($array['items']);
for ($x=0;$x<$nArrItems;$x++){
echo $array['items'][$x]['name'].'<br/>';
$nImages = count($array['items'][$x]['images']);
for($z=0;$z<$nImages;$z++){
echo $array['items'][$x]['images'][$z]."<br/>";
}
}
Result on my browser running Laravel
It has same result as with API tester
Result API tester
but if I compare it(the above) with chrome inspect
Chrome inspect
in my browser : "309adc074bfeef9ad6d13561ef2","70eb....
in API tester : "images":["309adc074bfeef9ad6d13561ef2","70eb....
in Chrome inspect : "images":["309adc074bfeef9ad6d13561ef2e3ad1","70eb....
It has missing characters that does not show in my browser & API Tester.
Any help as why this is happening would be greatly appreciated.

How to make active item of SideNav in another style?

I want the items in my SideNav by Kartik in the yii2 app to change the color when it is active(was clicked and open).
Sorry, I am pretty new for PHP and Yii and the question might be seen obvious but I really stack here.
I have already tried to use the "active" option that is explained in the documentation but it doesn't work. It doesn't show any error but is not working. I have a file adminMenu.php where the SideNav is written. and the panel.php view file where I showing it.
Also, I tried to add echo
$this->render('adminMenu'['currentPage'=>'admin/personal']);
but it shows error and thus I comment it for now.
adminMenu.php:
class adminMenu extends Widget{
public function init(){
$curentpage = Yii::$app->controller->id ;
parent::init();
ob_start();
echo SideNav::widget([
'type' => SideNav::TYPE_PRIMARY,
'headingOptions' => ['class'=>'head-style'],
'items' => [
['label' => 'Personal',
'url' => ['/admin/personal'],
'active' => ($curentpage == 'admin/personal')],
['label' => 'Clients',
'url' => ['/admin/clients'],
'active' => ($curentpage == 'admin/clients')],
...
]);
panel.php:
if(\Yii::$app->user->can('Admin')){
echo adminMenu::widget();
//echo $this->render('adminMenu'['currentPage'=>'admin/personal']);
}
i think that you mistake is in this line
$curentpage = Yii::$app->controller->id;
Yii::$app->controller->id only return the name of your controller , in this casi will be "admin" then you compare "admin" equals "controller/action" ('admin/personal') this never will be equals .
To do this , you can do a concat the actual controller and the actual action like this :
$curentpage = Yii::$app->controller->id.'/'.Yii::$app->controller->action->id;
and the comparation will be success and the "active" class add to you sidebar

Yii2 change breadcrumb url

There are two user types in my admin application. If an admin is logged in, there should be a param in each url like this:
Home>>Property>> view property
Current url : www.example.com/property/index
Desired Url : www.example.com/property/index?agentid=5
How can I achieve this?
This param value will be dynamic and only for admin.
In the top of your view file, (I consider view.php) You can use User::can() function to check for some admin/other permissions, and look at this example code to add param to link:
if (Yii::$app->user->can('admin')) {
$this->params['breadcrumbs'][] = ['label' => 'Property', 'url' => ['index', 'agentid' => 5]];
} else {
$this->params['breadcrumbs'][] = ['label' => 'Property', 'url' => ['index']];
}
$this->params['breadcrumbs'][] = $this->title;
It adds agentid=5 param if user has admin permission, and do not otherwise

Why my listview not found any record in yii2?

I have a dataprovider to search a world but my listview does not display any record? How can i send this query object to listview?
every thing in my webpage is worked very good but at the output my list view show "not found any result" this mean my list view code is no have problem . problem is in my dataprovider and this query beacuse i customize that
my controller:
$query = new Query();
$dataProvidersearch=new ActiveDataProvider([
'query'=>$query->from('tbl_post')->Where(['like', 'title', $search])-
>andWhere(['like', 'en_title', $search])->andWhere(['like', 'content', $search])->andWhere(['like', 'en_content', $search]),
]);
this is my list view in my view:
$posts = $model->getModels();
echo ListView::widget([
'dataProvider'=>$posts,
'itemView'=>'search',
'summary' => '',
'itemOptions' => [
'tag' => false
],
]);
I'm not sure you have enough code here for someone to help. Even something simple like a listview could consist of a view, a controller, and two model files and your code could be failing at any of these points. You may have simply forgot to include the listview library at the top of your view, but we can't see that in your current example.
What I would recommend is using Gii to generate a listview. It is simple to do and once you have it created, you can study the code to see where you went wrong. You can see how to get started generating code with Gii here: http://www.yiiframework.com/doc-2.0/guide-start-gii.html
ANSWER FROM COMMENTS: Replace andWhere with orWhere, no results are found because no record can match 'title' and 'en_title' and 'content' and 'en_content'.
You are submitting $posts as 'dataProvider' while it should be dataProvidersearch
Instead of:
$posts = $model->getModels();
echo ListView::widget([
'dataProvider'=>$posts,
'itemView'=>'search',
'summary' => '',
'itemOptions' => [
'tag' => false
],
];
Should be:
$posts = $model->getModels();
echo ListView::widget([
'dataProvider'=>$dataProvidersearch,
'itemView'=>'search',
'summary' => '',
'itemOptions' => [
'tag' => false
],
];

yii2 mpdf render html reached max memory size

I trying to render a very big pdf with mdpdf on Yii2 framework.
I genereate an html page, but when i call the render function, php run out of memory.
I don't wanna expand the memory_limit ini settings (256M is more than necessary).
I use this configuration, $html contains my huge code:
$pdf = new Pdf([
'mode' => Pdf::MODE_CORE,
'content' => $html,
'options' => [
'title' => 'Report',
],
'marginHeader' => 2,
]);
return $pdf;
Maybe there's a way to render step to step the pdf?
In Yii2 mpdf the content is normally a renderPartial for a form layout
and the renderPartial is populated by one or more models that are the result of query eg:
$models = MyModel::find()->all();
$content = $this->renderPartial('_mpdf_report_scheda', [
'model' => $models,
]);
$pdf = new Pdf([
.......
'content' => $content,
could be that in your case the result of the query retrieve to much rows so you could spliet you content in a part
eg: using limit() and offset()
$models = MyModel::find()
->limit(20)
->all();
$models = MyModel::find()
->limit(20)
.>offset(20)
->all();
and launch the pdf for parts