Is it possible to use two or more caching storage in yii2 framework? I already setup a Memcache for my web app but I wanted also to use a FileCache since I will be dealing with a large chunk of data.
hope someone can help. Thanks!
You can set any cache. Just set it at config file.
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'memCache' => [
'class' => 'MEMCACHE CLASS HERE',
],
.... ANY cache you want ...
]
You can register multiple cache application components. The component named cache is used by default by many cache-dependent classes (e.g. yii\web\UrlManager).
Official link
'components' => [
'cache' => [
'class' => 'yii\caching\MemCache',
],
'fileCache' => [
'class' => 'yii\caching\FileCache',
]
]
Related
I am trying to apply a theme to yii2 advanced application in frontend.
the code I am using to apply the theme is as follows:
'view' => [
'theme' => [
'pathMap' => ['#app/views' => '#app/themes/stargazers'],
'baseUrl' => '#web/../themes/stargazers',
],
],
pretty URL is turned on.
The physical location of theme is in the path like
frontend/themes/stargazers.
and stargazers folder is having this files and folders.
files/
layouts/
layouts/main.php
files/main_style.php
files/theme/
files/theme/main_style.css
files/theme/*.png
I have tried all path location, but was not able to apply the theme, may be missing something silly here.
Yii2 advanced template does not have #app this namespace by default, should you change to #frontend?
'view' => [
'theme' => [
'pathMap' => ['#frontend/views' => '#frontend/themes/stargazers'],
'baseUrl' => '#frontend/themes/stargazers',
],
],
I have MongoDB Active Records (models) and I'm wondering if it's possible to use Redis to automatically set/get/delete the models from Redis's storage.
For example, if I was to run:
MyModel::find()->where(["id" => 1])->one();
is there a way to make Redis store the result and return it the next time I run that same code?
Also, if I was to update the model with id = 1, I'd expect Redis to invalidate the cache.
Is all that possible?
It doesn't matter which DB to use. It is about how to implement them. Yii have those two components to set in the config file:
db: a database connection to be used where needed like an Active Record class to represent a model or Query Builders or ...
cache: designed to cache things from HTML pages and HTTP requests to database queries related data.
The good thing about MongoDB and Redis is that both can be used as a database connection or as a cache component. You may have those configs for example:
'components' => [
'db' => [
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://developer:password#localhost:27017/mydatabase',
],
'cache' => [
'class' => 'yii\redis\Cache',
'redis' => [
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
]
],
],
Here, while MongoDB is set as your default database, Redis is only used as a cache component and because all cache components have the same base class yii\caching\Cache they only support those APIs. Which should be fine if you are using it for caching proposes only.
Check the Yii2 Caching Guide to see all what you can do with a cache component. A quick example of what you are trying to do may be seen within #Blizz answer here where he did set an SQL query as a dependency to check if cached data should be used or invalidated instead.
In case you need to use the Redis database for more than just caching then you may have those configs instead:
'components' => [
'mongodb' => [
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://developer:password#localhost:27017/mydatabase',
],
'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
],
'cache' => [
'class' => 'yii\redis\Cache',
'redis' => 'redis' // id of the connection application component
],
],
Here we defined 2 databases and selected one of them to be also used as a cache component. It should work the exact same way except that you can also use the Redis database inside your app as a Redis ActiveRecord or a Redis ActiveQuery class. You will just need to set which DB to be used inside each model class as it is done in this example.
We configured like /var/www/app1 and /var/www/app2 , Both are logging in single session. How can I make this different session.
I tried with following solution from yii2 wiki. But it doesn't workout here.
'identityCookie' => [
'name' => '_backendUser', // unique for backend
'path'=>'/advanced/backend/web' // correct path for the backend app.
]
Please give solution for this issue.
Use a different session $name for each application. This can be set in your config as:
'components' => [
'session' => [
'class' => '\yii\web\Session',
'name' => 'mycustomname',
I am working on yii2 framework.This is new framework for me. I want to setup multiple language. I tried some way but didn't got success. Can anyone please suggest me simplest way ? What should i have to do ?
I am using this reference link
http://techisworld.com/working-with-multiple-languages-app-in-yii2-framework-using-i18n-system.html
1- For dynamic content (coming from database) I usually use this:
webvimark/multilanguage
It is very easy and isolated from your app DB tables structure and code, that gives flexibility in adding/removing languages at the long term.
2- For static content (words inside the markup) in frontend as an example:
add the lines in your frontend/config/main.php file,
'i18n' => [
'translations' => [
'app*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#app/messages',
'sourceLanguage' => 'en_US',
'fileMap' => [
'app' => 'app.php'
],
],
],
],
Put you translation file app.php file inside /frontend/messages, as any Yii translation file it returns an array of translations in a key-value pairs.
Then you can translate your static content using:
Yii::t('app', 'text to be translated')
In Yii 1.x you have the CWebLogRoute class to log all your queries and stuff in your browser. Is this option already available for Yii 2.0? I only see the FileTarget, DbTarget and EmailTarget classes in the framework.
The correct answer:
return [
'bootstrap' => ['debug'],
'modules' => [
'debug' => 'yii\debug\Module',
// ...
],
];
It's called debugger now in Yii 2.0. To enable it, put this in your configuration:
'preload' => ['debug'],
'modules' => [
'debug' => 'yii\debug\Module'
],