yii2 frontend and backend uses different sessions - session

Despite I did not separate sessions of frontend and backend apps on config files of those apps using session => ... option, my apps uses different sessions and when I login one of them then the other one is logouts. I could not find the source of problem. I want them using same session. What can be the problem?

Try this one
Add session in frontend->config->main.php
'components' => [
'session' => [
'name' => 'PHPFRONTSESSID',
'savePath' => sys_get_temp_dir(),
],
]
same in backend->config->main.php
'components' => [
'session' => [
'name' => 'PHPBACKSESSID',
'savePath' => sys_get_temp_dir(),
],
]

Related

How do I make Laravel write a log showing my problem?

I'm writing my first Laravel app that also includes Vue. I'm pretty new to both Laravel and Vue so please be gentle ;-) I'm using Laravel 8.4.x and Vue 2.6.12 on Windows 10.
In my very first axios invocation, I'm trying to write of a single record to a database table in a submit method of my Vue component, I'm getting Http status code 500, internal server error. The response in the console says that Laravel doesn't see a controller with the name ToDoController. I have no idea why that would be since I created it properly with php artisan and I can see it in VS Code.
In looking at other posts to try to understand this problem, I see that people recommend looking in the server logs to find out more information. I'm not sure if the logs will have information not found in the response situated in the browser console but I won't know until I look at it. The problem is that I don't know where to look for my log.
According to the Laravel docs, logging is governed by config/logging.php and that file says:
<?php
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;
return [
/*
|--------------------------------------------------------------------------
| Default Log Channel
|--------------------------------------------------------------------------
|
| This option defines the default log channel that gets used when writing
| messages to the logs. The name specified in this option should match
| one of the channels defined in the "channels" configuration array.
|
*/
'default' => env('LOG_CHANNEL', 'stack'),
/*
|--------------------------------------------------------------------------
| Log Channels
|--------------------------------------------------------------------------
|
| Here you may configure the log channels for your application. Out of
| the box, Laravel uses the Monolog PHP logging library. This gives
| you a variety of powerful log handlers / formatters to utilize.
|
| Available Drivers: "single", "daily", "slack", "syslog",
| "errorlog", "monolog",
| "custom", "stack"
|
*/
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single'],
'ignore_exceptions' => false,
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => env('LOG_LEVEL', 'debug'),
],
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => env('LOG_LEVEL', 'debug'),
'days' => 14,
],
'slack' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'username' => 'Laravel Log',
'emoji' => ':boom:',
'level' => env('LOG_LEVEL', 'critical'),
],
'papertrail' => [
'driver' => 'monolog',
'level' => env('LOG_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' => env('LOG_LEVEL', 'debug'),
],
'errorlog' => [
'driver' => 'errorlog',
'level' => env('LOG_LEVEL', 'debug'),
],
'null' => [
'driver' => 'monolog',
'handler' => NullHandler::class,
],
'emergency' => [
'path' => storage_path('logs/laravel.log'),
],
],
];
If I'm understanding this correctly, that means that errors at DEBUG or above are being written to logs/laravel.log, which I should be seeing in VS Code. But I do not even have a logs directory, let alone a laravel.log file.
Do I need to do something to create these logs in the first place as part of creating a new project? If so, what? If not, why don't I see my log? A server error is significant enough to get written into the log, right?
Also, while we're on the subject of logs, I have a chicken and egg question: does the value of LOG_CHANNEL in the logging.php file get set from the .env file or does the .env file get the value in the logging.php file? In other words, if I want to change my logging behaviour, which one do I alter?
The logs directory is located in the storage directory. So it's storage/logs/laravel.log.
Also, you can choose to log anything you want to using the Log facade. See laravel writing log messages [https://laravel.com/docs/8.x/logging#writing-log-messages]. Anything that is wrapped in the env() function is being pulled from the env file. The second param allows you to set a default if that value is not set or available in the env file. 1
#Donkarnash - I strongly suspect you've identified the problem. I'm not up on the new features of Laravel 8. What would my Route look like if I have a FQCN for the controller? I've tried all the variations I can think of but nothing works, including url('app/Http/Controllers/ToDoController')
Since Laravel 8 the default namespace App\Http\Controllers is not set in the RouteServiceProvider - which is a welcome change.
So now when defining routes in routes files say routes/web.php FQCN of the controller must be used
use App\Http\Controllers\ToDoController;
Route::get('/todos', [ToDoController::class, 'index']);
//OR without importing the use statement
Route::get('/todos', [\App\Http\Controllers\ToDoController::class, 'index']);
If you want you can also use namespace method for route groups
Route::namespace('App\Http\Controllers')
->group(function(){
Route::get('/todos', 'ToDoController#index');
Route::get('/todos/{todo}', 'ToDoController#show');
});
Using FQCN as in either importing use statement or inline also provides benefits of easy navigation and code suggestions in IDE's
To revert back to the old convention and set default namespace you should declare
$namespace in RouteServiceProvider
/**
* The controller namespace for the application.
*
* When present, controller route declarations will automatically be prefixed with this namespace.
*
* #var string|null
*/
protected $namespace = 'App\\Http\\Controllers';

Laravel 5 + memcached setup

I'm really just looking for an explanation about memecached and laravel. I understand what it does, but can I use my memcached installation with laravel. More specifically:
'memcached' => [
'driver' => 'memcached',
'persistent_id' => env('MEMCACHED_PERSISTENT_ID'),
'sasl' => [
env('MEMCACHED_USERNAME'),
env('MEMCACHED_PASSWORD'),
],
'options' => [
// Memcached::OPT_CONNECT_TIMEOUT => 2000,
],
'servers' => [
[
'host' => env('MEMCACHED_HOST', '127.0.0.1'),
'port' => env('MEMCACHED_PORT', 11211),
'weight' => 100,
],
],
],
I know/will set up the server aspect, and I get what the options do...but persistent_id, a memcached um and pw...what are they? Their uses? etc.. typically laravel is extremely well document but on memcached it says very little (And the little it does, seems to be dated and not based on 5.0 laravel)
Here is explanation from php.net:
By default the Memcached instances are destroyed at the end of the request. To create an instance that persists between requests, use persistent_id to specify a unique ID for the instance. All instances created with the same persistent_id will share the same connection.
http://php.net/manual/en/memcached.construct.php
So for your project just define a unique name for it.
Hope it helps.

Is modifying Laravel base config files a bad idea?

Laravel comes with some basic config files available in config directory.
which each file is basically an associated array.
now, I want to change the structure of config/mail.php from
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
to
'from' => [
'noreply' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
]
],
so I can have several emails with differnt name.
My question is generic here.
What are(if any) disadvantages of modifying structure of the config files?
Is it possible to be troublesome between LTS upgrades?
You shouldn't modify email.form data because Laravel uses the data:
You may specify a global from address in your config/mail.php configuration file. This address will be used if no other from address is specified within the mailable class

Configure Memcached in Laravel

I've tried to set up memcached with my Laravel
Set Driver
/config/app.php
'default' => 'memcached',
Configure Memcache Server
'stores' => [
'database' => [
'driver' => 'database',
'table' => 'caches',
'connection' => null,
],
'memcached' => [
'driver' => 'memcached',
'servers' => [
[
'host' => '127.0.0.1', 'port' => 11211, 'weight' => 100,
],
],
]
],
Cache
Route::get('cache/users', function() {
return Cache::remember('users', 60, function() {
return User::all();
});
});
How do I know I configure my caching with memcache properly ?
How do I see what I stored ?
First, you can use Cache::has('users') and Cache::get('users') in php artisan tinker or on a test route of some sort to see if stuff's being saved to the cache (and what the current contents are).
Second, you can connect to memcached (as well as other drivers like redis) via telnet and check directly.
telnet 127.0.0.1 11211
Once in, you can issue commands, like:
get users
which should spit out the contents of that cache key.
With the code you've shown, a non-working cache should also result in an exception. You can test this by turning off the memcached instance and refreshing the page - it should error out.
For the case when Memcache is on separate machine named memcached, i.e. docker with corresponding service: in .env set MEMCACHED_HOST=memcached

Yii2 Advanced app, different sessions for frontend and backend

I'm trying to set up a Yii2 advanced project. For this I used kartik-v's advanced app template. It works fine but (as He just mentioned here) if you log into the frontend and then go to backend you'll be logged in as well. So I would like to separate the frontend and backend logins (different sessions). I tried to configure the identity cookies but It didn't work. In the comments I found this: "Still, when either frontend or backend is signed in and we open the other, it shows automatically signed in because the session cookie is same, PHPSESSID."
So I changed the name and the savePath of the sessions in the config of frontend and the backend. With this it should work, but It doesnt. I got an 500 internal server error every time I go to my page. And if I try to log in, it just doesnt work, it redirects me but does not log me in. I found out that If I dont set the savePath I dont get the error but still nothing happens. And If check in the 'remember me' option I get the error message but the login works.. So I dont know what to do now. My main config files:
backend:
'components' => [
'session' => [
'name' => 'backend_sessid',
'savePath' => __DIR__ . '/../tmp',
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_backendUser',
'path' => '/projectname/backend/web'
]
],
frontend:
'components' => [
'session' => [
'name' => 'frontend_sessid',
'savePath' => __DIR__ . '/../tmp',
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_frontendUser',
'path' => '/projectname'
]
],
One approach would be to use the Role Based Access Control described here: http://www.yiiframework.com/doc-2.0/guide-security-authorization.html
This way, you would set different roles for frontend users and backend users. If a user with different privileges tried to access the wrong site area, you could log him out and redirect him to the login page.

Resources