My session config file says to use memcached, but all artisan commands are loading the "array" driver instead. I'm writing a web sockets application with Ratchet and need to connect to Memcached to get the user's session information, but seems to ignore my config.
Where and how does Laravel determine which session drivers to use for Artisan commands?
According to Illuminate\Support\ServiceProvider\SessionServiceProvider::setupDefaultDriver() Laravel will set the session driver to array if running in console.
You can easily override this by registering your custom service provider. Create a custom service provider, extend the default session service provider and override the method setupDefaultDriver). Here is my custom service provider for example:
<?php namespace App\Console;
use Illuminate\Session\SessionServiceProvider as DefaultSessionProvider;
class SessionServiceProvider extends DefaultSessionProvider
{
protected function setupDefaultDriver() {}
}
Then open up config/app.php and replace 'Illuminate\Session\SessionServiceProvider' with 'App\Console\SessionServiceProvider'.
Now artisan will also use the same session storage as Laravel app.
Since you are trying to attach the session to Ratchet, You can directly inject this session instance into Ratchet app:
$session = new \Ratchet\Session\SessionProvider(
new MyCustomRatchetApp(),
$this->getLaravel()['session.store']
);
$server = new \Ratchet\App('localhost');
$server->route('/sessDemo', $session);
$server->run();
Gufran posted a good answer, but another solution which doesn't involve swapping out a service provider, it just to set the default driver manually in your artisan command (or tests).
Like so: Session::setDefaultDriver('memcached');. You can do the same for cache: Cache::setDefaultDriver('memcached');. You do this before doing any other commands, of course, so that it doesn't start using the array.
If you need to, you can manually put in connection information: Config::set('cache.memcached', array(array('host' => '127.0.0.1', 'port' => 12345, 'weight' => 100)));
Related
im using a package called https://github.com/garygreen/pretty-routes
there is line in its service provider boot() method (here the code)
it is defining a get route with middlewares from its config file(link to the code) I just added 'auth:web' to its config file but it seems the 'auth:web' middleware is called as soon as code reaches the line before Laravel bootstraps its session and etc. when the auth('web')->user() is yet null
What I can not understand is that I do the same exact thing (here the code)with laravel/telescope but it works. why ???
also changing :
Route::get(config('pretty-routes.url'), 'PrettyRoutes\PrettyRoutesController#show')
->name('pretty-routes.show')
->middleware(config('pretty-routes.middlewares'));
to :
$this->app['router']->get(config('pretty-routes.url'), 'PrettyRoutes\PrettyRoutesController#show')
->name('pretty-routes.show')
->middleware(config('pretty-routes.middlewares'));
in service provider seems to solve the problem and make this code behave like the way telescope package use 'auth:web' as middleware.
what's happening ?
You need to have the web middleware applied to any routes you need sessions for, which is what the default authentication system is using. When you apply the auth middleware without this it can't possibly resolve a user since there is no session to be authenticated against.
You need to apply the web middleware and then what ever other middleware you want:
'middlewares' => [
'web', 'auth:web',
],
If you look at the telescope example you provided you will see they also add the web middleware. So you didn't quite "do the same exact thing" as the telescope config.
I removed the database connection information from the env file and database.php file.
And I set the database dynamically with each request from the client.
I do this in main middleware
$origin = $request->header('Origin');
\Config::set(['database.default' => 'mysql']);
\Config::set(['database.connections.mysql.host' => '127.0.0.1']);
\Config::set(['database.connections.mysql.database' => $origin]);
\Config::set(['database.connections.mysql.username' => 'root']);
\Config::set(['database.connections.mysql.port' => '3306']);
I have no problem with the login and the token is created. But in other actions, I get a 401 error
In fact, the passport calls the connection information to the database from the env file or database.php. But we want to do this through middleware, or we can set the database information for it before checking auth: api.
By default, Passport access default DB. If you would like to use dynamic database add below code in the boot method of your app/Providers/AppServiceProvider.php:
public function boot()
{
Config::set('database.connections.pgsql.database', 'newdatabase');
DB::purge('pgsql');
DB::reconnect('pgsql');
}
The documentation Here suggests php artisan passport:client --client for creating a client, but I want to do it using a controller, or ideally, using the native JSON apis provided by passport.
Is that possible at all or will I have to override the methods in Passport::client ?
Old question, but here's a 2021 answer for people finding this on Google.
I find calling Artisan commands from code unsavory, especially as #kishore-kadiyala mentioned because you get back printed output, rather than code.
Instead, you can use the Laravel\Passport\ClientRepository class to create a client directly:
use Laravel\Passport\ClientRepository;
// ... snip ...
$clients = App::make(ClientRepository::class);
// 1st param is the user_id - none for client credentials
// 2nd param is the client name
// 3rd param is the redirect URI - none for client credentials
$client = $clients->create(null, 'My Client Name', '');
Here, $client is a Laravel\Passport\Client instance directly. This is exactly how the Artisan command creates the clients anyway.
You can do
Artisan::call('passport:client --client');
Look at Artisan
I follow up this tutorial using Laravel 5.4 Creating your first Laravel app and adding authentication
But I can't retrieve Auth0 users, the registration works fine in my dashboard new users are created in Auth0 server, but not in my local database and also I can't dd(Auth0::getUser());
I get this error:
Class 'Auth0\Login\LoginServiceProvider' not found
I've added this
Auth0\Login\LoginServiceProvider::class
in my provider array and
'Auth0' => Auth0\Login\Facade\Auth0::class
in my aliases.
I did all steps on configuration from Auth0 docs: Auth0 PHP (Laravel) SDK
I'm out off option now!
Ok, figure it out now.
The error was coming from 'AppServiceProvider.php'
Just import those two classes in AppServiceProvider.php as:
use Auth0\Login\Contract\Auth0UserRepository as Auth0UserRepositoryContract;
use Auth0\Login\Repository\Auth0UserRepository as Auth0UserRepository;
And then your Register method should look like this:
public function register()
{
$this->app->bind( Auth0UserRepositoryContract::class, Auth0UserRepository::class );
$this->app->bind( Auth0UserRepositoryContract::class, Auth0UserRepository::class );
}
if you want to persist data in your database then Create your own Repository and update the bind method YourCustomerRepositoryContract::class, YourCustomerRepository::class
Just like that.
For more info about creating your own Repository
Auth0 PHP (Laravel) SDK Quickstarts: Login
I have a service named setting which fetches use's preference from somewhere. I need use it in configuring some other service. For example I need to get user's email address email host.
Using 'host' => app('setting')['email']['host']), in config/mail.php file,
does not work. It seems that the app() services are not accessible in that stage.
What is the correct way to do that?
You could change a config at runtime within a service provider.
Let's just use the app/Providers/AppServiceProvider which is shipped with Laravel. So in the boot() method you could do any changes to the config like this:
public function boot()
{
$this->app->make('config')->set('whatever.config', 'This has changed');
}