Not Found Page Working On Backend But Not Working at Frontend Yii2 - yii

I have yii installed on my live server we have two interfaces one backend and one frontend. In backend we add products and in front end the view is rendered for the products. The problem is that for example this is our website https://mmstore.be/products/714/IPhone-12-Mini-Scherm-Reparatie product page if you put anything at the last of the url like this https://mmstore.be/products/714/IPhone-12-Mini-Scherm-Reparatie/03003300303094949949494 the page will remain the same means it should be showing not found page but the same page comes again.
Below is my config file code frontend.
<?php
$params = array_merge(
require __DIR__ . '/../../common/config/params.php',
require __DIR__ . '/../../common/config/params-local.php',
require __DIR__ . '/params.php',
require __DIR__ . '/params-local.php'
);
return [
'id' => 'app-frontend',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'frontend\controllers',
'components' => [
'request' => [
'csrfParam' => '_csrf-frontend',
],
'cache' => [
'class' => 'yii\caching\FileCache',],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
],
'session' => [
// this is the name of the session cookie used for login on the frontend
'name' => 'advanced-frontend',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'baseUrl' => '',
'enablePrettyUrl' => false,
'showScriptName' => false,
'enableStrictParsing' => true,
'rules'=>array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'class' => 'yii\web\UrlNormalizer',
'collapseSlashes' => true,
'normalizeTrailingSlash' => true,
),
],
],
'params' => $params,
];
i have error.php in my site directory and below is its code
<?php
/* #var $this yii\web\View */
/* #var $name string */
/* #var $message string */
/* #var $exception Exception */
use yii\helpers\Html;
$this->title = $name;
?>
<h1><?= Html::encode($this->title) ?></h1>
<div class="alert alert-danger">
<?= nl2br(Html::encode($message)) ?>
</div>
<p>
The above error occurred while the Web server was processing your request.
</p>
<p>
Please contact us if you think this is a server error. Thank you.
</p>

It is strange that you disabled "enablePrettyUrl", and using pretty url. If "/products/" is a controller name and assuming "IPhone-12-Mini-Scherm-Reparatie" is a parameter named "url" then you can change urlManager like this:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'/'=>'site/index',
'/products/<id>/<url>'=>'products/view',
'/products/<id>'=>'products/view',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
in controller it should be something like this
public function actionView($id, $url) {
$product = Product::find()->where(['id'=>$id])->one();
// ...
}

Related

duplicate pages issue on installed script website yii framework

i want to specify that i have almost no experience with php and yii framework
I am trying to setup a classified ads website with yii ,but after i did some seo audit i realised that almost all of my category pages have duplicates like this :
example.com/category?slug=cars -this url is a duplicate that i don't want
example.com/category/cars -this is the url that i want to have
This is my urlManager code :
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
'search' => 'site/search',
'contact' => 'site/contact',
'<controller:conversation>/<action:delete>' => '<controller>/<action>',
'<controller:conversation>/<action:reply>/<conversation_uid:[a-z0-9_\-]+>' => '<controller>/<action>',
'<controller:account>/<action:invoices>/<page:\d+>' => '<controller>/<action>',
'<controller:account>/<action:conversations>/<page:\d+>' => '<controller>/<action>',
'<controller:listing>/<action:index|update|package|preview>/<slug:[a-z0-9_\-]+>' => '<controller>/<action>',
'page/<slug:[a-z0-9_\-]+>' => 'pages/index',
'<controller:category>/<action:location|map-view|get-map-location>' => '<controller>/<action>',
[
'pattern' => 'category/<slug:[a-z0-9_\-]+>/<page:\d+>',
'route' => 'category/index',
],
[
'pattern' => 'category/<slug:[a-z0-9_\-]+>',
'route' => 'category/index',
],
[
'pattern' => 'category/map-view/<slug:[a-z0-9_\-]+>/<page:\d+>',
'route' => 'category/map-view',
],
[
'pattern' => 'category/map-view/<slug:[a-z0-9_\-]+>',
'route' => 'category/map-view',
],
[
'pattern' => 'store/<slug:[a-z0-9_\-]+>/<page:\d+>',
'route' => 'store/index',
],
[
'pattern' => 'store/<slug:[a-z0-9_\-]+>',
'route' => 'store/index',
],
'<url:.+/>' => 'site/redirect'
],
],
If you need any further detailes or specific code ask me and i will try to provide !
Thanks

access to subfolders controller and view in YII2

hi i built a controller in a folder inside controllers folder
i tryed to access my controller and its view but i couldnt always error 404
please tell me what is the problem
here is the details
this is SiteUserController in Controllers/userzone/ folder
namespace app\controllers\userzone;
use yii\web\Controller;
use app\models\UserZone;
/**
* Default controller for the `dashboard` module
*/
class SiteUserController extends Controller
{
/**
* Renders the index view for the module
* #return string
*/
public function actionIndex()
{
$id = \Yii::$app->user->id;
$model = UserZone::find()->where(['id_zone'=>$id])->with('user')->one();
// $model->joinWith('companiesCompany');
return $this->render('siteuser/index',[
'model'=>$model
]);
}
}
the view file is in Views/siteuser/index.php directory .
i changed url manager to
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'userzone/<controller:\w+>/<action:\w+>'=>'userzone/<controller>/<action>',
],
],
In Controller:
return $this->render('index',[
'model'=>$model
]);
Its Working
You need to change you controllerNamespace in config/main file i.e. below code for config/main.php
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-frontend',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'app\controllers\userzone', //here is your controller path
'components' => [
'view' => [
'theme' => [
'pathMap' => [
'#frontend/views' => '#themes/frontend/views',
],
],
],
'request' => [
'csrfParam' => '_csrf-frontend',
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
],
'session' => [
// this is the name of the session cookie used for login on the frontend
'name' => 'advanced-frontend',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
/*
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
*/
],
'params' => $params,
];

in yii2 : why my default language is en

how to change default language from en to another language
$config = [
'on beforeAction' => function ($event)
{
Yii::$app->language = 'fa';
},
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'language' => '/fa',
'components' => [
'jdate' => [
'class' => 'jDate\DateTime'
],
'mycomponent' => [
'class' => 'app\components\MyComponent',
],
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => '******',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'urlManager' => [
'class' => 'codemix\localeurls\UrlManager',
// Disable index.php
'languages' => ['fa', 'en'], // List all supported languages here
'showScriptName' => true,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
// '' => 'site/index/fa',
// '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
// '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => require(__DIR__ . '/db.php'),
/*
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
*/
],
'params' => $params,
];
this is my $config in web.php
i want set default to 'fa' in root site !
my site automatic change lang to 'en' in first time in any browser!
i try to change lang with
'on beforeAction' => function ($event)
{
Yii::$app->language = 'fa';
},
but that is dosent correct work!
Remove this ridiculous 'on beforeAction' thing and just set
'language' => 'fa', // NOT '/fa'!
سلام
ویدئو های آقای صیف زاده رو مشاهده کردین؟
لینک زیر آموزش های ویدئویی ایشون هست . در شماره های 17 و 18 و 19
این موضوع رو آموزش دادن.
https://drive.google.com/drive/folders/0B4ZlNlar4Ij6XzJrbVZOejRCcGM

Automatically refreshing yii2 grid after server database changes

I have a grid that involves truck verification. I would like the contents of the grid to change without page refresh.
Currently this works when the page is refreshed but i would like it to work even when a page is not refreshed that is when the content changes in the server database.
This is the grid
<?php
$gridColumns = [
['class' => 'kartik\grid\SerialColumn'],
'reg_no',
[
'attribute'=>'truck_category',
'value'=>'truckCategory.category'
],
[
'class' => 'kartik\grid\ActionColumn',
'vAlign'=>'middle',
'urlCreator' => function($action, $model, $key, $index) { return '#'; },
'viewOptions'=>['title'=>"vdgdv", 'data-toggle'=>'tooltip'],
'updateOptions'=>['title'=>"update", 'data-toggle'=>'tooltip'],
'deleteOptions'=>['title'=>"delete", 'data-toggle'=>'tooltip'],
]
];
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'containerOptions' => ['style'=>'overflow: auto'], // only set when $responsive = false
'beforeHeader'=>[
[
'options'=>['class'=>'skip-export'] // remove this row from export
]
],
'toolbar' => [
[],
'{export}',
'{toggleData}'
],
'pjax' => true,
'bordered' => true,
'striped' => false,
'condensed' => false,
'responsive' => true,
'hover' => true,
'floatHeader' => true,
'showPageSummary' => true,
'panel' => [
'type' => GridView::TYPE_PRIMARY
],
]);
?>
This is the controller that renders the grid
public function actionTrackingcenter()
{
$query = Truck::find()->where(['truck_status'=>5]);
$searchModel = new TruckSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams,$query);
return $this->render('trackingcenter/index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
Could someone give a direction on how i can achieve the automatic change
You can use below with set-time-interval with jQuery
<?php
$this->registerJs('
setInterval(function(){
$.pjax.reload({container:"YOUR_PJAX_CONTAINER_ID"});
}, 10000);', \yii\web\VIEW::POS_HEAD);
?>
This code will be place after your GridView code
This will be reload your Pjax grid every 10 second

Yii GroupGridView buttons

I use this extension(GroupGridView) in my yii project.
Now, I need help. How to add crud buttons for the grouped elements, to get something like this:
I don't really know is it right sollution.
I've created component GroupButtonColumn:
class GroupButtonColumn extends CButtonColumn {
public $name = 'buttons';
public $value; }
And my view
$this->widget('ext.groupGridView.GroupGridView', array(
'dataProvider' => $model->search(),
'summaryText' => false,
'filter' => $model,
'mergeColumns' => ['t_id', 'buttons'],
'columns' => [
[
'name' => 't_id',
'header' => '№',
'value' => 'CHtml::link($data->t_id, ["/task/view", "id"=>$data->t_id])',
'filter' => false,
'type' => 'raw'
],
/* ... */
[
'class' => 'GroupButtonColumn',
'template' => '{update}, {delete}',
'buttons' => [
'update' => [
'url' => '$this->grid->controller->createUrl("/task/update", array("id"=>$data->t_id))'
],
'delete' => [
'url' => '$this->grid->controller->createUrl("/task/delete", array("id"=>$data->t_id))'
]
]
],
],
));
Try this way:
'mergeColumns' => array('project_id', 'project_id')
/* ... */
'columns' => [
'project_id',
'id',
/* ... */
[
'name' => 'project_id',
'header' => 'Action',
'type'=>'raw',
'value' => function($data){
return 'Start';
}
],