I want to access session or Yii::$app in config file like ( config/main.php or config/main-local.php) !
Is that possible or not?
I want to check session and make some style available!
'components' => [
'assetManager' => [
'assetMap' => [
'r.css' => Yii::$app->session['lang'] ? 'css/styleltr.css' : 'css/stylertl.css' ,
],
],
or how do something like that in assetmanager????
You can't access user session in your config file. But, you could use a conditional in your layout and register different assets based on session values.
try accessing what you need this way
\Yii::$app->getAssetManager()->assetMap;
Related
I have an admins array as a variable which is now declared in ViewComposer. However, now I want to use the same variable in middlewares too. Where is the best way to place a variable so I can share it through other files?
What I mean by ViewComposers:
$admins = ['hello#example.com'];
Where should I define it so that I can access it both in Middleware and ViewComposer? Or maybe completely globally through the app?
(If it was a string, I'd use .env but it doesn't accept arrays)
You can use configuration-values
$value = config('admins.list');// change admins and list is the key array returned
To set configuration values at runtime, pass an array to the config helper:
config(['admins.list' => [ 'admis list' ] ]);
admins would be inside config folder as admins.php
admins.php is something like following
return [
'list' => [
'admin1',
'admin2'
],
'other settings' => true
];
I'm using sessions for the first time in Laravel and I'm trying to do a multiple step form, so I thought using sessions would be a smart move. however the following code returns a null value, what am I doing wrong?
$user_information = [
"name" => $request->name,
"email" => $request->email,
"remember_token" => $request->_token,
"password" => bcrypt($request->password),
"role_id" => 3
];
session('user_signup', $user_information);
dd(session('user_signup'));
In your controller you can save variable into session like
session()->put('user_signup',$user_information);
For checking your session variable in controller
session()->has('user_signup','default value');
For deleting your session variable in controller
session()->forget('user_signup');
For checking your session variable if it exists in blade and printing it out
#if(session()->has('user_signup'))
session()->get('user_signup')
#endif
I have tested this already and struggling along then I realized that I should never use dd() (the dump and die method) after using the session() because your are blocking the system from writing on the session() cookie file.
I'm not really sure about that but it works for me .. let me know if this is True.
Try this
session(['user_signup'=> $user_information]);
or
session()->put('user_signup',$user_information);
and you can check session by logging it
Log::info(Session::get('user_signup'));
check your log file it should be there.
Laravel docs link - https://laravel.com/docs/5.4/session#storing-data
first : you put something in a session
second : check the storage/framework/session folder , if your session work fine you can see your session data in a session folder now.
if you save a session and session folder is still empty :
first change the 'driver' => env('SESSION_DRIVER', 'file')
to 'driver' => env('SESSION_DRIVER', 'array') and 'driver' => env('SESSION_DRIVER', 'database')
second set the storage/framework/session permission to 755
and finally go to your kernel file and add bellow code in 'api'
'api' => [
//add this bellow two line
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,
'throttle:60,1',
'bindings',
],
then check your session folder again and if you put something in any session you should now see them in this folder, you can delete files in session folder, use the session again to save something in it , going back to session folder and see the session folder is not empty anymore , and you're done, image of the session folder
Right now I'm trying to implement themming for my Yii2 based project.
How I see the thing now:
User chooses an application theme from the list on the settings
page in backend.
Using yii2-settings I'm saving all the
configuration data in DB (pretty easy).
In the application
bootstrap.php I'm creating new alias called #theme. Basically it
should lead us to a application theme base path (used in search
paths, assets manager, e.t.c.).
According to official
documentation, that's how I configured my view component:
'view' => [
'theme' => [
'basePath' => '#theme',
'baseUrl' => '#theme',
'pathMap' => [
'#app/views' => '#theme',
'#app/widgets' => '#theme/widgets',
'#app/modules' => '#theme/modules',
],
],
],
An issue I have is with p.3. According to yii2-settings documentation that's how I supposed to read the settings:
$theme = Yii::$app->settings->get('name', 'general');
Yii::setAlias('#theme', realpath(dirname(__FILE__)."/../../themes/$theme"));
But obviously, it's not working for me because of yii2-settings component didn't initialized yet when bootstrap.php is called. I've been trying to initialize it later in the init() method of my base controller, then adjust other aliases manually, but I feel that way being somewhat 'unclean', and also it still fails because of #theme alias is also used in asset file which is Yii2 starting to publish before calling the controller's init method.
So does anyone has any thoughts of how to do that 'hacking' the code as less as possible? I know I could just move configuration to some file, then read it manually before the application initialization, but it's still not the way I want to go.
Maybe there's some way to override some system component to set the alias after db component is loaded, but before view component configuration? Or Yii loads this components in a different order? Anyway. Any help would be appreciated!
You could try an Application Event in bootstrap:
\Yii::$app->on(\yii\base\Application::EVENT_BEFORE_REQUEST, function ($event) {
$theme = Yii::$app->settings->get('name', 'general');
Yii::setAlias('#theme', realpath(dirname(__FILE__)."/../../themes/$theme"));
});
OR in configuration file:
[
'on beforeRequest' => function ($event) {
// ...
},
]
From Yii 2 docs:
EVENT_BEFORE_REQUEST This event is triggered before an application
handles a request. The actual event name is beforeRequest.
When this event is triggered, the application instance has been
configured and initialized. So it is a good place to insert your
custom code via the event mechanism to intercept the request handling
process. For example, in the event handler, you may dynamically set
the yii\base\Application::$language property based on some parameters.
Here's the final solution:
config/bootstrap.php:
// Setting a temporary path for components configuration - will be changed later
Yii::setAlias('#theme', realpath(dirname(__FILE__)."/../../themes/"));
config/main.php
'components' => [
'view' => [
'theme' => [
'basePath' => '#theme',
'baseUrl' => '#theme',
'pathMap' => [
'#app/views' => '#theme',
'#app/widgets' => '#theme/widgets',
'#app/modules' => '#theme/modules',
],
],
],
],
'on beforeRequest' => function ($event) {
$theme = Yii::$app->settings->get('theme', 'general');
Yii::setAlias('#theme', realpath(dirname(__FILE__)."/../../themes/$theme"));
},
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',
is it possible to change the default view path for mails in Yii 2?
Reading this and it seems the only way you can change the view path is by calling:
setViewPath()
..but I don't want to have to call that every time I want to write code to send mail; I would prefer to just be able to change the default view path. Can this be done and if so how?
Yes, you can change default view path in the application config:
'components' => [
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '...', // Insert path here, you can use aliases
],
],