Laravel Delete Event Listener - laravel

A while ago I added all events and listeners related to the auth system, as defined in the docs here, and generated all the listeners. I would now like to use only two listeners and clean up the Listeners folder.
So in EventServiceProvider I've commented out what I don't need:
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
'Illuminate\Auth\Events\Registered' => [
'App\Listeners\LogRegisteredUser',
],
// 'Illuminate\Auth\Events\Attempting' => [
// 'App\Listeners\LogAuthenticationAttempt',
// ],
// 'Illuminate\Auth\Events\Authenticated' => [
// 'App\Listeners\LogAuthenticated',
// ],
'Illuminate\Auth\Events\Login' => [
'App\Listeners\LogSuccessfulLogin',
],
// 'Illuminate\Auth\Events\Failed' => [
// 'App\Listeners\LogFailedLogin',
// ],
// 'Illuminate\Auth\Events\Logout' => [
// 'App\Listeners\LogSuccessfulLogout',
// ],
// 'Illuminate\Auth\Events\Lockout' => [
// 'App\Listeners\LogLockout',
// ],
// 'Illuminate\Auth\Events\PasswordReset' => [
// 'App\Listeners\LogPasswordReset',
// ],
// 'Illuminate\Auth\Events\Verified' => [
// 'App\Listeners\LogVerifiedUser',
// ],
];
I then delete all Listeners in the app/Listeners folder.
If I then run php artisan event:generate I get an error:
ErrorException : include(/PATH
HERE/vendor/composer/../../app/Listeners/LogRegisteredUser.php):
failed to open stream: No such file or directory
What am I missing?

I had the same problem. you may run command
php artisan clear-compiled
or,
composer dump-autoload
and then run php artisan event:generate

Related

Fresh install laravel 8 event not showing in laravel websocket dashboard

Before I posted this question, I checked other SO/articles with the same problem but did not really fixed anything. My freshly installed Laravel 8 application is not registering the events in dashboard though I can see the events if in Laravel.log file. This is the tutorial that I was following. In .env file, if I change the BROADCAST_DRIVER value to log, I can see in the log file the following:
local.INFO: Broadcasting [chatapplication] on channels [public.chatapplication.1] with payload:
{
"test": 123,
"data": "send data",
"socket": null
}
Below is the code that I tried to follow in the tutorial:
.env
BROADCAST_DRIVER=log # or pusher
QUEUE_CONNECTION=sync
# App\Events\ChatApplication
class ChatApplication implements ShouldBroadcast
{
public function broadcastOn()
{
// return new PrivateChannel('channel-name');
$info = new Channel('public.chatapplication.1');
Log::info($info);
return $info;
}
public function broadcastAs()
{
return "chatapplication";
}
.....
}
#config/broadcasting.php
'connections' => [
....
'pusher' => [
....
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'useTLS' => false,
'encrypted' => false,
'host' => '127.0.0.1',
'port' => 6001,
'scheme' => 'http'
],
],
....
];
I am trying to trigger the events in both php artisan tinker and in route and still nothing.
#web.php
Route::get('/chat-application', function () {
event(new \App\Events\ChatApplication("send data"));
});
The same event(new \App\Events\ChatApplication("send data")) in tinker . returned empty array.
The expected output should be events is displayed in dashboard. I read about queues and other things on other post but I dont really now where I should change. I am running in windows with wamp server

how to add category for laravel permissions?

I created an application using the spatie permission package.
And I want to categorize the permissions as shown below, without using the package
Open config/permission.php and define all of the permission in it using nested array:
'default_permissions' => [
'product_management' => [
'view_own_pro',
'view_pro',
'mng_own_pro',
'mng_pro',
'force_delete_pro',
'restore_pro',
],
'role_management' => [],
'category_management' => [],
'order_management'=> [],
'user_management'=> [],
],
Edit answer:
Or use it more simply:
'default_permissions' => [
// role_management
'view_role',
// category_management
'view_own_category',
'force_delete_category',
// user_management
'view_user',
'create_user',
// pro_management
'view_own_pro',
'view_pro',
'mng_own_pro',
'mng_pro',
'force_delete_pro',
'restore_pro',
],
And in routing:
Route::group(['middleware' => ['permission:view_user|view_role']], function () {
//
});
Or in blade:
#can('view_user')
//
#endcan
Check spatie-docs for more info.

How to set different auth guard in laravel with auth0

I am work on laravel with auth0 project (package is auth0/login).
My project has local auth users.
I am going to add auth method with auth0, so I have set auth config for auth0.
This is my /config/auth.php code
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'auth0web'=>[
'driver' => 'session',
'provider' => 'auth0_users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'auth0_users'=>[
'driver' => 'auth0'
],
],
this is my callback route's controller function.
public function auth0SigninCallback()
{
//return response()->json(Auth:: guard('auth0web)->user());
$service = \App::make('auth0');
// Try to get the user information
$profile = $service->getUser();
$userRepo = new Auth0UserRepository();
$auth0User = $userRepo->getUserByUserInfo($profile);
return response()->json($auth0User);
}
Auth::guard('auth0web)->user();
At this code I am getting null for auth user.
$profile is correctly getting for now.
how can I get different guard(Auth::guard('auth0web')->user()) for auth
Thanks for your advance.
PS
This is my session data maybe that is correctly
{
"_token": "OAmhhYKWeO0tK6E5FN3DHaRvVYx6PP7z0YPMAPrz",
"_previous": {
"url": "http://xxxxx.dev/login"
},
"_flash": {
"old": [],
"new": []
},
"auth0__user": {
"sub": "auth0|xxxxxxxxxxx",
"nickname": "xxxxxxxx",
"name": "xxxxx#xxxxx.com",
"picture": "https://s.gravatar.com/avatar/xxxx.png",
"updated_at": "2019-03-26T17:28:53.981Z",
"email": "xxxxxx#ixxx.com",
"email_verified": true
}
}
I have try override callback route controller
this is code
// Get a handle of the Auth0 service (we don't know if it has an alias)
$service = \App::make('auth0');
// Try to get the user information
$profile = $service->getUser();
// Get the user related to the profile
$auth0User = $this->userRepository->getUserByUserInfo($profile);
if ($auth0User) {
// If we have a user, we are going to log them in, but if
// there is an onLogin defined we need to allow the Laravel developer
// to implement the user as they want an also let them store it.
if ($service->hasOnLogin()) {
$user = $service->callOnLogin($auth0User);
} else {
// If not, the user will be fine
$user = $auth0User;
}
Auth::guard('auth0web')->login($user, $service->rememberUser());
}
// dd(auth());
// \Log::info(auth()->user());
return \Redirect::intended('/home');
}
at this time auth() is correctly working
but while redirecting to home Auth is initialized
this is my custom middleware.
public function handle($request, Closure $next)
{
// \Log::info('------------here---------');
// dd(auth());
$auth = Auth::user() ?? Auth::guard('ldap')->user();
// dd(auth());
// \Log::info(auth()->user());
if (!isset($auth->id)) {
return redirect('login');
} else {
return $next($request);
}
}
At this part I'm getting null from auth()->user()

Laravel config file of a module

I need to get the keys of a file inside the config of a module. I created User module using nWidart package. This is the registerConfig function
protected function registerConfig()
{
$this->publishes([
__DIR__.'/../Config/config.php' => config_path('user.php'),
], 'config');
$this->mergeConfigFrom(
__DIR__.'/../Config/config.php', 'user'
);
}
I created a file named menu.php that returns an array with a key items, then tried this
dd(config('user::config.name'))
dd(config('user::menu.items'))
They both return null. I also tried php artisan vendor-publish of UserServiceProvider but it still returns null. How can I return the keys?
update
If I simply do: config() I get a full array of the config arrays and can see my package config file there. But the menu config file is not in there
In Your serviceProvider, register method.
$this->mergeConfigFrom(
__DIR__ . '/../config/master.php',
'master'
);
// Merge configs
$this->mergeConfigFrom(
__DIR__ . '/../config/slave.php',
'slave'
);
Create two files inside config folder
slave.php
<?php
return [
'menu' => [
'OP1',
'OP2',
'OP3',
'OP4',
]
];
master.php
//any other config you want to load
Run php artisan config:cache
and use
dd(config('slave.menu'));
dd(config('master.XXX'));
This will output
array:4 [▼
0 => "OP1"
1 => "OP2"
2 => "OP3"
3 => "OP4"
]
return [
'name' => 'Blog',
'en_name'=>'Blog',
'icon'=>'comment',
'color1'=>'EF6F6C',
'color2'=>'EF6F6C',
'route'=>'blog',
'disable' => false,
'subs'=>[
['name'=>'All Posts','route'=>'blog/list'],
['name'=>'All Categories','route'=>'category/list']
]
];

How can I call Artisan::call(...) more than once? (Migrate and then Seed)

I have found that using the Artisan::call() command to migrate and then again to seed results in a proper migration but the database is never seeded. This may be a bug or perhaps there is a way to flush the previous command.
For example:
Artisan::call('migrate', [
'--database' => 'tenant',
'--path' => 'database/tenantMigrations',
'--force' => true,
]);
And then:
Artisan::call('db:seed', [
'--database' => 'tenant',
'--class' => 'TenantSeeder',
]);
As you can see, I am running these commands on a freshly created tenant DB to "provision" it. Each of these commands works separately, but not together.
I have tried looking for further documentation on joining the two commands while being able to specify the class of the seeder. This would potentially look like:
Artisan::call('migrate', [
'--database' => 'tenant',
'--path' => 'database/tenantMigrations',
'--force' => true,
'--seed' => true,
'--class' => 'TenantSeeder', // this is the only one I can't do, which is critical
]);
I have also tried running the seeder like:
(new \TenantSeeder)->run();
I get the error: Call to a member function line() on null.
It is also interesting to note that all works properly on my local Homestead environment, but not on a Digital Ocean server managed by Forge.
Edit
My current solution is to put my seeder logic inside of regular classes (that do not extend the base Seeder class) and call these as I displayed above.
You can create a wrapper command with all the options needed, then call the migrate, seed, etc commands individually:
php artisan make:command MigrateAndSeedCommand
The class may look something like:
class MigrateAndSeedCommand extends Command
{
protected $signature = 'migrateandseed {--database=} {--path=} {--force} {--seed} {--class=}';
public function handle()
{
Artisan::call('migrate', [
'--database' => $this->option('database'),
'--path' => $this->option('path'),
'--force' => $this->option('force'),
]);
Artisan::call('db:seed', [
'--database' => $this->option('database'),
'--class' => $this->option('class'),
]);
}
}
Usable via:
php artisan migrateandseed --database=tenants --path=database/tenantMigrations --force --seed --class=TenantSeeder
// or
Artisan::call('migrateandseed', [
'--database' => 'tenant',
'--path' => 'database/tenantMigrations',
'--force' => true,
'--seed' => true,
'--class' => 'TenantSeeder',
]);
EDIT
Use $this instead of Artisan, as well as optionally queueing the commands:
// or $this->queue
$this->call('db:seed', [
'--database' => $this->option('database'),
'--class' => $this->option('class'),
]);
Calling Commands From Other Commands

Resources