How do I configure the session in my cakephp application? - session

Im trying create a endpoint for a session to my application. I follow all the steps in https://book.cakephp.org/3.0/en/development/sessions.html
But dont works.
I create the table im my database called session and the ComboSession in src/Http/Session/ComboSession.php and change the conigurations in app.php like the documentation of cakephp suggests.
'Session' => [
'defaults' => 'database',
'handler' => [
'engine' => 'ComboSession',
'model' => 'session',
'cache' => 'apc'
]
],
'Cache' => [
'apc' => ['engine' => 'Apc']
]
I tried this too:
'Cache' => [
'apc' => ['engine' => 'Apcu']
]
My error message: Cache engine Cake\Cache\Engine\ApcuEngine is not properly configured.

My souluction:
'Session' => [
'defaults' => 'database',
'handler' => [
'engine' => 'ComboSession',
'model' => 'sessions',
'cache' => 'defaults'
]
],
'Cache' => [
'defaults' => [
'className' => 'Cake\Cache\Engine\FileEngine',
'path' => CACHE,
'url' => env('CACHE_DEFAULT_URL', null),
],

Related

Integration laravel 7 log to slack

I tried to integration error log laravel to slack notification. But when i tested to send log it can't send massage to slack. I followed this tutorial https://panjeh.medium.com/send-laravel-6-log-to-slack-notification-573a6d95a14e. And I've tested on route too
Here is it the config.php
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single','slack'],
'ignore_exceptions' => false,
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
],
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'days' => 14,
],
'slack' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'username' => 'Laravel Log',
'emoji' => ':boom:',
'level' => 'warning',
],
Here is it the route to test
Route::get('slack', function() {
Log::error('Test');
return 'Slack notif';
});
I've got the url too and have it put in .env LOG_SLACK_WEBHOOK_URL=
You can change default log channels to slack.So you have to set
LOG_CHANNEL=stack
Also you can specify channels. Instead of changing channels
Log::channel('slack')->inf("test");
or you can do
Log::stack(['daily', 'slack'])->info("test");
or you can specify channels inside daily array.So that no need to change anythink
'stack' => [
'driver' => 'stack',
'channels' => ['daily', 'slack'],
'ignore_exceptions' => false,
],

Stream current log to storage folder

I am running my Laravel 7 application for testing purposes on a shared host.
My logging.php configuration looks like the following:
<?php
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;
return [
'default' => env('LOG_CHANNEL', 'single'),
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['daily'],
'ignore_exceptions' => false,
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'error',
],
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'days' => 14,
],
'slack' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'username' => 'Laravel Log',
'emoji' => ':boom:',
'level' => 'critical',
],
'papertrail' => [
'driver' => 'monolog',
'level' => 'debug',
'handler' => SyslogUdpHandler::class,
'handler_with' => [
'host' => env('PAPERTRAIL_URL'),
'port' => env('PAPERTRAIL_PORT'),
],
],
'stderr' => [
'driver' => 'monolog',
'handler' => StreamHandler::class,
'formatter' => env('LOG_STDERR_FORMATTER'),
'with' => [
'stream' => 'php://stderr',
],
],
'syslog' => [
'driver' => 'syslog',
'level' => 'debug',
],
'errorlog' => [
'driver' => 'errorlog',
'level' => 'debug',
],
],
];
I only get the log file from the day before in my storage folder. However, I would like to get the logs that are created when the application runs, like a log stream.
Any suggestions how to do this?
I appreciate your replies!
so far, I am new to logs too. The two lines below can be added to any controller
use Log; #on top along with the other 'use' statements
this 2nd line would be inserted as needed:
Log::info('File: '. __FILE__. ' This would get logged on file: storage/laravel.log');
The result is appended onto laravel.log and would be similar to:
[2020-07-11 05:08:38] local.INFO: File: C:\<someFolders>\laravel\vendor\laravel\framework\src\Illuminate\Routing\Route.php This would get logged on file: storage/laravel.log
'hope it helps.

Cakephp different sessions for different users

I have cakephp application which has two separate logins one for admin and one for employee. I am using session to store login details.but when i logged out from admin it automatically logouts from employee and vice versa. can i use two different sessions for that?
I am not using role based login. Instead i am using plugin for admin
admin App Controller
$this->loadComponent('Auth', [
'loginAction' => ['controller' => 'Users', 'action' => 'login'],
'logoutRedirect' => [
'controller' => 'Users',
'action' => 'login',
],
'authenticate' => [
'Form' => [
'userModel' => 'Users',
'fields' => ['username' => 'username', 'password' => 'password']
]
],
'authError' => 'Enter Credentials',
'storage' => 'Session',
'unauthorizedRedirect' => false
]
);
Employee App controller
$this->loadComponent('Auth', [
'loginAction' => [
'controller' => 'Employees',
'action' => 'loginemp'
],
'logoutRedirect' => [
'controller' => 'Employees',
'action' => 'loginemp',
],
'authError' => 'Enter Credentials',
'authenticate' => [
'Form' => [
'userModel' => 'Employees',
'fields' => ['username' => 'username', 'password' => 'password']
]
],
'storage' => 'Session',
'unauthorizedRedirect' => false
]);
Change
'storage' => 'Session'
to
'storage' => [ // Add this array
'className' => 'Session',
'key' => 'Auth.Admin',
],
in your admin panel Auth cofiguration.
For e.g. Assuming below admin panel Auth configuration:
$this->loadComponent('Auth', [
'loginAction' => ['controller' => 'Users', 'action' => 'login'],
'logoutRedirect' => [
'controller' => 'Users',
'action' => 'login',
],
'authenticate' => [
'Form' => [
'userModel' => 'Users',
'fields' => ['username' => 'username', 'password' => 'password']
]
],
'authError' => 'Enter Credentials',
'storage' => [ // Change this
'className' => 'Session',
'key' => 'Auth.Admin',
],
'unauthorizedRedirect' => false
]
);

Issue setting new Yii2 theme on basic template

I have downloaded and set the theme according to the Themefactory site and other SO posts but the theme is just loading as pure html. Meaning that there is no formatting, just text and links.
This is my code in the web.php file in the config directory:
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'view' => [
'class' => 'yii\web\View',
'theme' => [
'class' => 'yii\base\dorian',
'pathMap' => ['#app/views' => 'themes/dorian'],
'baseUrl' => 'themes/dorian'
],
],
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'KecrcGLWURmyPrWp3QBgYuOQ21JuCFA1',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
Screenshot of file tree: http://prntscr.com/9708gi
Does anyone see anything wrong???
try
'components' => [
'view' => [
'theme' => [
'basePath' => '#app/themes/tf-dorian',
'pathMap' => [
'#app/views' => '#app/themes/tf-dorian',
],
'baseUrl' => '#web/',
],
],
please try this and let me know if it doesn't work:
'view' => [
'class' => 'yii\web\View',
'theme' => [
'class' => 'yii\base\Theme',
'pathMap' => ['#app/views' => 'themes/tf-dorian'],
'baseUrl' => 'themes/tf-dorian'
]
]
Thanks. I figured it out. The themes directory had to go under the "web" directory.

Yii2 Advanced app, different session for frontend and backend with subdomains

I have a problem with my Yii2 app.
I have a advanced-app with frontend and backend parts on different domains (subdomain). I use webvimark user management module, but I think the problem is not in it.
Frontend app -> domain.com
Backend app -> admin.domain.com
So I have problem with login in backend, it is not working.
I enter login and password, and after submit form I see login form again.
'user' => [
'identityClass' => 'webvimark\modules\UserManagement\models\User',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_backendIdentity',
'domain' => 'admin.domain.com',
],
'class' => 'webvimark\modules\UserManagement\components\UserConfig',
],
and
'session' => [
'name' => 'BACKENDSESSID',
'cookieParams' => [
'domain' => 'admin.domain.com',
],
],
Any ideas?
Update #1: My config located: /backend/config/main.php
Update #2: There was a problem when transferring backend on a subdomain
Okey, there was a problem with the configuration of the module, as well as the wrong config in the frontend.
Backend:
'user' => [
'identityClass' => 'webvimark\modules\UserManagement\models\User',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_backendIdentity',
'domain' => 'backend.test.dev',
],
'class' => 'webvimark\modules\UserManagement\components\UserConfig',
'on afterLogin' => function ($event) {
\webvimark\modules\UserManagement\models\UserVisitLog::newVisitor($event->identity->id);
},
],
'session' => [
'name' => 'BACKENDSESSID',
'cookieParams' => [
'domain' => 'backend.test.dev',
],
],
Frontend:
'user' => [
'identityClass' => 'webvimark\modules\UserManagement\models\User',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_frontendIdentity',
'path' => '/',
],
'class' => 'webvimark\modules\UserManagement\components\UserConfig',
'on afterLogin' => function ($event) {
\webvimark\modules\UserManagement\models\UserVisitLog::newVisitor($event->identity->id);
}
],
'session' => [
'name' => 'FRONTENDSESSID',
'cookieParams' => [
'path' => '/',
],
],

Resources