Answer the question
In order to leave comments, you need to log in
How to get authorized user from package route in Laravel 5?
Hello colleagues! Please help me figure out the problem.
There is some functionality that I decided to transfer to the extension package.
The problem is that I cannot get an authorized user from the package routes.
In the application, he is authorized, I follow the route from the package, auth()->user() returns null.
Help me understand why?
I took out the directory, put it together according to con non, connected it in the composer of the project
"repositories": [
{
"type": "path",
"url": "packages/kdes70/chatter",
"options": {
"symlink": true
}
}
],
"require": {
"php": ">=7.1.3",
"davejamesmiller/laravel-breadcrumbs": "^5.0",
"fideloper/proxy": "~4.0",
"laravel/framework": "5.6.*",
"laravel/tinker": "~1.0",
"laravelcollective/html": "^5.6",
"predis/predis": "^1.1",
"pusher/pusher-php-server": "~3.0",
"spatie/laravel-fractal": "*",
"kdes70/chatter": "*"
},
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"Kdes70\\Chatter\\": "src/",
"Kdes70\\Chatter\\ChatterController": "src/Http/Controllers/"
},
"files": [
"helper/helpers.php"
]
},
"extra": {
"laravel": {
"providers": [
"Kdes70\\Chatter\\ChatterServiceProvider"
],
"aliases": {
"Chatter": "Chatter\\Facades\\Chatter"
}
}
}
class ChatterServiceProvider extends ServiceProvider
{
public function boot(): void
{
Relation::morphMap(config('chatter.relation'));
$this->publishes([
$this->configPath() => config_path('chatter.php'),
$this->componentsPath() => base_path('resources/assets/js/components/chatter'),
], 'chatter');
$this->loadRoutesFrom($this->routesPath());
$this->loadViewsFrom($this->viewsPath(), 'chatter');
$this->loadMigrationsFrom($this->migrationsPath());
$this->registerBroadcast();
}
public function register(): void
{
$this->mergeConfigFrom($this->configPath(), 'chatter');
$this->registerFacade();
$this->registerChatter();
$this->registerAlias();
}
protected function registerBroadcast(): void
{
Broadcast::channel(
$this->app['config']->get('chatter.channel.chat_room') . '-{conversationId}',
function ($user, $conversationId) {
if ($this->app['conversation.repository']->canJoinConversation($user, $conversationId)) {
return $user;
}
}
);
}
protected function registerFacade(): void
{
$this->app->booting(function () {
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Chatter', Chatter::class);
});
}
protected function registerChatter(): void
{
$this->app->bind('chatter', function ($app) {
$config = $app['config'];
$conversation = $app['conversation.repository'];
return new ChatterService($config, $conversation);
});
}
protected function registerAlias(): void
{
$this->app->singleton('conversation.repository', function () {
return new ConversationRepository();
});
$this->app->alias('conversation.repository', ConversationRepository::class);
}
protected function viewsPath(): string
{
return __DIR__ . '/../resources/views/chatter';
}
protected function configPath(): string
{
return __DIR__ . '/config/chatter.php';
}
protected function componentsPath(): string
{
return __DIR__ . '/../resources/assets/js/components';
}
protected function migrationsPath(): string
{
return __DIR__ . '/database/migrations';
}
protected function routesPath(): string
{
return __DIR__ . '/Http/routes.php';
}
public function provides(): array
{
return [
'conversation.repository',
];
}
}
Route::group([
'namespace'=>'Kdes70\Chatter\Http\Controllers',
'prefix' => 'messages',
'middleware' => ['web', 'auth'],
], function (){
Route::get('/', '[email protected]')->name('messages');
Route::get('/chat/{conversation_id}', '[email protected]')->name('chat');
});
Answer the question
In order to leave comments, you need to log in
The bottom line is that in the controller in the actions auth ()-> user works, but not in the constructor, Why is that?
most likely you are trying to get the user too early
try calling auth()->user later. If not later, then write
If API, then in the constructor
public function __construct(Request $request)
{
$this->middleware(['auth:api']);
$this->user = \Auth::guard('api')->user();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question