I would like for my application to automatically change template
so i created this structure frontend/web/themes/myTheme
following http://www.yiiframework.com/doc-2.0/guide-output-theming.html i added this code in frontend/config/main.php
'components' => [
'view' => [
'theme' => [
'basePath' => '#app/themes/myTheme',
'baseUrl' => '#web/themes/myTheme',
'pathMap' => [
'#app/views' => '#app/themes/myTheme',
],
],
],
],
however i kept getting the error that " /var/www/html/myProject/app/frontend/views/site/index.php" The view file does not exist???
i also tried to put this function inside the controller based on How to change default view path in Yii2?
public function getViewPath()
{
return Yii::getAlias('#web/themes/myTheme/site');
}
so my question is:
1. how can I change the views default path?
2. how can i do it automatically since i can not change the common/config/main.php settings during a session?
site controller
class SiteController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['index'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['index'],
'allow' => true,
'roles' => ['#'],
],
],
],
];
}
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
/**
* Displays homepage.
*
* #return mixed
*/
public function actionIndex()
{
$searchModel = new ProductSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
}
I think you are configuring the wrong file.Don't configure themes in the common/config
Try this:
in frontend/config/main.php
'components' => [
'view' => [
'theme' => [
'pathMap' => [
'#frontend/views'=>'#frontend/themes/myTheme',
],
],
],
],
if you need to configure the backend then in the backend/config/main.php
'components' => [
'view' => [
'theme' => [
'pathMap' => [
'#backend/views'=>'#backend/themes/myTheme',
],
],
],
],
The common folder is has to contain the files that are required by both
frontend and backend.
Hope this helps.
First question:
I think than you have a common mistake in yii when used advanced app: the alias #app references root directory of frontend, backend, or common depending on where you access it from View documentation here.
You would used the solution proposed by ovicko.
Second question:
You can change the theme configuration dynamically in controller through view object:
$this->view->theme->pathMap =['#app/views' => '#app/themes/myTheme/',];
EDIT
According to Documentation:
Theming is a way to replace a set of views with another without the need of touching the original view rendering code.
What means that the original view file must exist and theming simply replace it in during rendering. So you must create a file in /var/www/html/myProject/app/frontend/views/site/index.php (a empty file is valid) in order to theming works.
This sounds quite ridiculous, I Know, but it works.
But I think that is much better and easier the use of differents layouts, again, to change dinamically the layout in your controller:
$this->layout = 'route/yourlayout';
Related
Related to How to add custom wizards in typo3 7 TCA? how can costum wizards in TYPO3 9 be implemented? I've added my entry to the Routes.php
return [
'tx_csseo_preview' => [
'path' => '/wizard/tx_csseo/preview',
'target' => \Clickstorm\CsSeo\UserFunc\PreviewWizard::class . '::render'
],
'tx_csseo_permalink' => [
'path' => '/wizard/tx_csseo/permalink',
'target' => \Clickstorm\CsSeo\UserFunc\PermalinkWizard::class . '::render'
]
];
How can I add them now to my TCA field?
'tx_csseo_title' => [
'label' => 'LLL:EXT:cs_seo/Resources/Private/Language/locallang_db.xlf:pages.tx_csseo_title',
'exclude' => 1,
'config' => [
'type' => 'input',
'max' => $extConf['maxTitle'],
'eval' => 'trim',
'fieldWizard' => [
'tx_csseo_preview' => [
'disabled' => false,
]
]
]
],
This does not work. What do I miss? Thanks in advance.
Related to your kind of wizard the registration-process is different and extensive explained here. You can leave the entries in Routes.php away (perhaps even the whole file if nothing else is inside).
Registration is done in ext_localconf.php:
$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'][1485351217] = [
'nodeName' => 'importDataControl',
'priority' => 30,
'class' => \T3G\Something\FormEngine\FieldControl\ImportDataControl::class
];
Then reference the new wizard in TCA:
'somefield' => [
'label' => $langFile . ':pages.somefield',
'config' => [
'type' => 'input',
'eval' => 'int, unique',
'fieldControl' => [
'importControl' => [
'renderType' => 'importDataControl'
]
]
]
],
Then finally the class with the "magic"
declare(strict_types=1);
namespace T3G\Something\FormEngine\FieldControl;
use TYPO3\CMS\Backend\Form\AbstractNode;
class ImportDataControl extends AbstractNode
{
public function render()
{
$result = [
'iconIdentifier' => 'import-data',
'title' => $GLOBALS['LANG']->sL('LLL:EXT:something/Resources/Private/Language/locallang_db.xlf:pages.importData'),
'linkAttributes' => [
'class' => 'importData ',
'data-id' => $this->data['databaseRow']['somefield']
],
'requireJsModules' => ['TYPO3/CMS/Something/ImportData'],
];
return $result;
}
}
In the linked example there is still an Ajax Route with corresponding files, including a special defined route, but that's not required to get the basic wizard shown.
Concerning the registration in ext_localconf.php there is above the number 1485351217 as array-key shown. For an own registered node just calculate once the current time as unix-timestamp and enter that instead. So it's unique and can't be mistaken with other definitions of any registered nodes.
In contrast to the linked example I used slightly different descriptions, so I call the process in ext_localconf.php registering, and the inclusion in TCA referencing. Perhaps this small difference makes it a bit more clear.
Icons
Concerning Icons there is still a difference to earlier TYPO3 versions, they have to be registered now too and in TCA they are only referenced too by the registered name. Here in the TCA-file is no icon referenced but the class below makes usage of it. Here is an example how an icon has to be registered in ext_tables.php:
$systemIconRegistry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Imaging\IconRegistry::class);
$systemIconRegistry->registerIcon(
'imagemapwizard_link_edit',
\TYPO3\CMS\Core\Imaging\IconProvider\BitmapIconProvider::class,
[
'source' => 'EXT:imagemap_wizard/Resources/Public/Icons/link_edit.png'
]
);
The new icon registry is implemented starting with TYPO3 version 7.5
Don't forget the configuration in YourExtension/Configuration/Backend/AjaxRoutes.php. See the documentation
I installed dektrium user but when override the AdminController.php and tried to reach admin/index what I get is Forbidden(403). After overriding the behaviors to:
'rules' => [
[
'allow' => true,
'roles' => ['?'],
],
],
the error is still the same. Did this because I still don't have any roles. What can cause this behavior ? I am aiming at the origin index.php ( the one in the dektrium\yii2-user module). Thank you!
You need to follow these rules to override controllers for dektrium-user
directory structure
You can change the following if you want it into the frontend, only starting folder needs to be changed
- backend
- controllers
- user
- AdminController
Your config for the user module under the module section should look like following
'modules' => [
..............
'user' => [
'controllerMap' => [
'admin' => 'backend\controllers\user\AdminController' ,
] ,
For overriding the controller with a new action index your minimum code should look like below
AdminController
<?php
namespace backend\controllers\user;
use dektrium\user\controllers\AdminController as BaseAdmin;
class AdminController extends BaseAdmin {
public function behaviors() {
$behaviours = parent::behaviors ();
$behaviours['access']['rules'][] = [
'allow' => true ,
'actions' => [ 'index' ] ,
'roles' => [ '?' ]
];
return $behaviours;
}
public function actionIndex(){
return $this->render('index');
}
}
When I try to login OR Signup it does not work. If I try to login with wrong credential it works. But if I use right credential it gives error:
Object configuration must be an array containing a "class" element.
Error on line:
static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
and related line
$models = $this->createModels($rows);
My config is as below
main.php
return [
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
],
];
main-local.php
return [
'components' => [
'mongodb' => [
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://mts:123456#localhost:27017/mangodb',
],
'user' => [
'identityClass' => 'common\models\User', // This is your class with IdentityInterface
'enableAutoLogin' => true,
],
],
];
Make sure the ActiveRecord object you are using (the one in static::findOne(...)) is an instance of yii\mongodb\ActiveRecord and not yii\db\ActiveRecord because the latter uses standard db component I assume you have not got configured.
we follow this youtube link to create yii2+mongodb connection but not working. check this link: https://www.youtube.com/watch?v=1msu95ZkRe8
gives an error Object configuration must be an array containing a "class" element.
in video user.php model copy paste from doc file what is that file please help me.
I successfully realized the login service at my own project using MongoDB. But I didn't use video tutorial you pointed out.
It looks like you have an error in your configuration file. I would first check the config for mongodb component. It must be something like following (yii2-mongodb project README)
return [
//....
'components' => [
'mongodb' => [
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://developer:password#localhost:27017/mydatabase',
],
],
];
Other component is worth checking out is the user component:
return [
//....
'components' => [
'user' => [
'identityClass' => 'frontend\models\User', // This is your class with IdentityInterface
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
],
];
But in fact, similar error may appear for any configuration element of YII2.
as per video change below function behaviors in comman/models/User.php
public function behaviors()
{
return [
'timestamp' => [
'class' => 'yii\behaviors\TimestampBehavior',
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
]
]
];
}
How is it possible to extend yii\rest\UrlRule in a way I can rewrite rules for actions of a controller? For example, I want to generate the following URIs:
/user/[username]
/user/keywords
/user/keyword/[key1]/[key2]/[...]
...
Every above actions are rendering their own view too.
You don't need to extend yii\rest\UrlRule. Just add your rules to routes of UrlManager by putting them on extraPatterns property of yii\rest\UrlRule.
For example suppose You defined a list action in your controller:
class BarController extends Controller
{
public $modelClass = 'app\models\Foo';
public function actionList()
{
return ['id' => 1];
}
}
Then in configuration file add extra route:
<?php
// some configs are here
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => [
'v1/bar',
],
'extraPatterns' => [
'GET list' => 'list',
],
],
],
],
// and some other configs are here
Now you can browse the api with /v1/bars/list. Read Yii2 Documentations for more examples.
you must change the extends of Controller in ActiveController
class ArticleController extends ActiveController