Laravel Storage::extend not working - php

I can't figure out where I'm going wrong, with this. I've followed the Laravel docs by installing spatie/flysystem-dropbox via composer copied the DropboxServiceProvider from the Laravel Docs, added the service provided to the config\app.php, ran composer dump autoload but yet I am still getting the following error message:
PHP error: Undefined index: driver in /***/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemManager.php on line 112
Here is the service provider:
<?php
namespace App\Providers;
use Storage;
use League\Flysystem\Filesystem;
use Spatie\Dropbox\Client as DropboxClient;
use Illuminate\Support\ServiceProvider;
use Spatie\FlysystemDropbox\DropboxAdapter;
class DropboxServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*
* #return void
*/
public function boot()
{
Storage::extend('dropbox', function ($app, $config) {
$client = new DropboxClient(
$config['authorizationToken']
);
return new Filesystem(new DropboxAdapter($client));
});
}
/**
* Register bindings in the container.
*
* #return void
*/
public function register()
{
//
}
}
And here's my config/app/php:
...
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
App\Providers\DropboxServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
...
Finally, here's my config/filesystems.php:
'dropbox'=>[
'authorizationToken'=>env('DROPBOX_ACCESS_TOKEN')
],

Turns out that I was missing the driver value in the config/filesystems/php so it should have been this:
'dropbox'=>[
'driver' => 'dropbox', <=== THIS WAS MISSING
'authorizationToken'=>env('DROPBOX_TOKEN')
],

One good Pacakge avilable for extend storage using dropbox:
detail documentation : https://github.com/GrahamCampbell/Laravel-Dropbox
steps for use this package :
1 : composer require graham-campbell/dropbox in your cmd window
2 : after this you have to register service provider for laravel dropbox,
go to config/app.php
and add 'GrahamCampbell\Dropbox\DropboxServiceProvider' this in provider array
and 'Dropbox' => 'GrahamCampbell\Dropbox\Facades\Dropbox' this to aliases array
3: now you have to publish pacakge so 'php artisan vendor:publish' run this in your cmd
- this will create dropbox.php file in your config in this file you have to add your credentials
4: here you have two option for connection you can use it according to your choice.
usage :
simple example :
use GrahamCampbell\Dropbox\Facades\Dropbox;
// you can alias this in config/app.php if you like
Dropbox::createFolder('Folder_Name');
// we're done here - how easy was that, it just works!
Dropbox::delete('Folder_Name');
// this example is simple, and there are far more methods available

Related

Laravel 5.2 dynamic environment variables based on user

In my .env file I have two variables
App_id: 12345
App_secret: abc123
But I'm wondering if there's a way so that if user userNo2 logs in then it would instead use
App_id: 45678
App_secret: abc456
Is there a way to have if/else functionality in the env file based on the user?
Yes it is possible, but not in the .env file. Instead, you can move your logic to middleware:
Step 1: Add default values to the application config
Open your app/config/app.php and add your default values to the existing array.
<?php
return [
'APP_ID' => '45678',
'APP_SECRET' => 'abc456',
...
];
Step 2: Create a new Middleware
php artisan make:middleware SetAppConfigForUserMiddleware
Edit the file to look like this:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
class SetAppConfigForUserMiddleware
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
$authorizedUser = Auth::user();
if (!App::runningInConsole() && !is_null($authorizedUser)) {
Config::set('app.APP_ID', 'appidOfUser' . $authorizedUser->name);
Config::set('app.APP_SECRET', 'appsecretOfUser' . $authorizedUser->email);
}
return $next($request);
}
}
Step 4: Run your middleware
If you need to set this config for the user in all the web routes you can add to the $middlewareGroups array in app/Http/kernel.php. This will apply the middleware to all the routes inside web.php.
/**
* The application's route middleware groups.
*
* #var array
*/
protected $middlewareGroups = [
'web' => [
...
\App\Http\Middleware\SetAppConfigForUserMiddleware::class,
],
Step 5: Testing
For example, my Auth:user()->name is "John" and my Auth:user()->email is "john#example.com"
If you put this in your resources/views/home.blade.php
App Id Of User <code>{{config('app.APP_ID')}}</code>
App Secret Of User <code>{{config('app.APP_SECRET')}}</code>
The result will be appidOfUserJohn and appsecretOfUserjohn#example.com.
.env can only store key-value.
Since .env is always used by config, you can use Config::set('app.id', 45678); to mutate the env at run time. You can place the code in your middleware, and the value will back to default after the request ends.

Route Error: Symfony 4 No route found for "GET /blog"

I am having this error:
Symfony\Component\HttpKernel\Exception\NotFoundHttpException: No route
found for "GET /blog" (from "http://localhost:8000/produits")
I added the annotation #Route in the method in my controller (like I saw in other website):
/**
* #Route("/blog", name="article.index")
* #return Response
* */
public function index():Response
{
return $this->render("blog/article.html.twig", [
"current_menu" => 'articles'
]);
}
I tried to add methods={"GET","HEAD"} in #Route but I have the same error
How do I solve this problem?
This one worked for me (adding / to the end of route path):
/**
* #Route("/blog/", name="article.index")
* #return Response
* */
public function index():Response
{
return $this->render("blog/article.html.twig", [
"current_menu" => 'articles'
]);
}
You will also at least need use Symfony\Component\Routing\Annotation\Route; at the start of the file (among the other 'use' lines), Some framework setup for Route annotations could also need to be enabled. Also, how does blog/article.html.twig refer to the route or path?
A freshly installed Symfony 4 instance will need composer require doctrine/annotations package, unless its already been installed by some other package.
https://symfony.com/doc/current/routing.html#creating-routes has more details.

Symfony 4 and DoctrineEncryptBundle - Where define yaml configuration?

I am trying to encrypt data for a medical application that's how i found DoctrineEncryptBundle (https://packagist.org/packages/michaeldegroot/doctrine-encrypt-bundle)
I am still a rookie with symfony 4 and the documentation give the method for what seems to be previous Symfony version.
I already downloaded the bundle (composer require michaeldegroot/doctrine-encrypt-bundle)
For step 2 : "Enable the database encryption bundle"What is explain in document vs what I did in SF4
Which seems correct.
Then, there is no config.yml in SF4 and I don't know where to define the configuration (encryptor class and the path to the key file).
This yaml =>
ambta_doctrine_encrypt:
encryptor_class: Halite # or Defuse
secret_directory_path: '%kernel.project_dir%' # Path where to store the keyfiles
The documentation : https://github.com/michaeldegroot/DoctrineEncryptBundle/blob/master/Resources/doc/configuration.md
My files
Entity Patient (which i want to encrypt)
`
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Ambta\DoctrineEncryptBundle\Configuration\Encrypted;
/**
* #ORM\Entity
* #ORM\Entity(repositoryClass="App\Repository\PatientRepository")
* #ORM\Table(name="patient")
*/
class Patient {
/**
* #var string
* #Encrypted
* #ORM\Column(type="string")
*/
private $nom;`
The controller :
public function ajouterPatient(Request $request)
{
$patient = new Patient();
$form = $this->createForm(PatientType::class, $patient);
if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
$em = $this->getDoctrine()->getManager();
$patient->setNomsAffichage($patient->getNom()." ".$patient->getPrenom());
$patient->setActif(true);
$em->persist($patient);
$em->flush();
return $this->redirectToRoute('menu_patients');
}
return $this->render('Patients/ajouterPatient.html.twig', array(
'form' => $form->createView(),
));
}
I guess i should define something in service.yml like a link with the bundle but i don't know how.
Any help would be greatly appreciated.
I know this is an old question but for the people who still want to know where to define the configuration yaml file in Symfony 4 for this bundle, here is the answer I got it working with.
In the file config/bundles.php
Add at the end this line to define the Ambta Symfony bundle:
return [
...
Ambta\DoctrineEncryptBundle\AmbtaDoctrineEncryptBundle::class => ['all' => true]
];
Create a new yaml file in: config/packages/orm_services.yaml
Here you can put the configuration. For example:
ambta_doctrine_encrypt:
encryptor_class: Halite
secret_directory_path: '%kernel.project_dir%'

Laravel 5: How can I load database values into the config/services.php file?

I have a multi-tenant app I'm working on and while adding the socialite package, I tried to load the custom facebook client_id and client_secret for the specific website from the database. I can't really use the env variables because each site will have it's own custom facebook keys.
It seems you can't really call a model's method on the config/services.php file because it might not have been loaded yet. I've tried going through the request lifecycle docs to resolve this to no avail.
I've also tried to create a service provider to get the value from my Business model's method and set it as a constant but still, by the time it's available in the app, the config/services.php file has been loaded.
Here's where I want the database value available:
config/services.php
'facebook' => [
'client_id' => \App\Business::getAppKeys()->fb_client_id,
'client_secret' => 'your‐fb‐app‐secret',
'redirect' => 'http://your‐callback‐url',
],
Error:
Fatal error: Call to a member function connection() on null
Here's a really quick example of what I'm talking about.
Using the following, I am able to query the database, grab some details and write them to a config file, before the rest of my application is initialised.
See how I'm able to access the value in a route?
// app/Providers/ConfigServiceProvider.php
namespace App\Providers;
use App\Models\Country;
use Illuminate\Support\ServiceProvider;
class ConfigServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
$countries = Country::pluck('name', 'iso_code');
config()->set(['app.countries' => $countries->toArray()]);
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
// config/app.php
// ...
'providers' => [
// ...
App\Providers\ConfigServiceProvider::class,
// ...
],
// ...
// routes/web.php
Route::get('config', function () {
dd(config('app.countries'));
});
You really should not want to do this. Initialising DB connections from a model requires all config to be loaded, and you intend to use these connections to define your config. You've found yourself having a circular dependency problem.
I'd like to suggest having a look at the socialite package you're trying to use. If no facilities exist in the service to set/override credentials at runtime, see if you're able to extend the service with your own implementation that does allow for that. I think that will be the only way to accomplish what you're trying to do.
Besides all that, config files are supposed to be composed of only scalar values and arrays, so that they can be cached.

Laravel 5's route helper doesn't work on fresh install

I am following a tutorial sayings that Laravel has a helper that permits to write the routes like that :
<?php
get('/', function () {
return view('welcome');
});
Instead of :
<?php
Route::get('/', function () {
return view('welcome');
});
(The "Route::" prefix is missing in the first one).
Since what i've looked the documentation (where I've found nothing really related but the providers involved), I correctly have in my providers :
'providers' => [
/*
* Laravel Framework Service Providers...
*/
(...)
Illuminate\Foundation\Providers\FoundationServiceProvider::class,
(...)
/*
* Application Service Providers...
*/
(...)
App\Providers\RouteServiceProvider::class,
(...)
],
And the tutorial says that it has to work in a fresh install.
The router helper functions were removed in December. You can see the changes here:
https://github.com/laravel/framework/commit/62cbae78ba2d40944892c5a16f2d2463087bce23
In the upgrade guide, you can see what is deprecated and removed.
The get, post, and other route helper functions have been removed. You may use the Route facade instead.
Source: https://laravel.com/docs/5.2/upgrade

Categories