Laravel 5 - Storage Adapter Extending Will Not Work - php

For some reason I cannot extend the Storage Class like the example in the docs.
I have been trying to create an FTP adapter because it is required for the project I am working on.
Here is the Service Provider I have created
<?php namespace herbals\Providers;
use Storage;
use Illuminate\Filesystem\FilesystemServiceProvider as ServiceProvider;
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Ftp as FTPAdapter;
class FTPFileSystemServiceProvider extends ServiceProvider {
public function boot()
{
Storage::extend('ftp', function($app, $config)
{
$client = new FTPAdapter(array(
'host' => env('FTP_HOST') || '',
'username' => env('FTP_USERNAME') || '',
'password' => env('FTP_PASSWORD') || '',
'port' => env('FTP_PORT') || '',
'root' => env('FTP_ROOT') || '/',
'passive' => true,
'ssl' => true,
'timeout' => 30,
));
$filesystem = new Filesystem($client);
return $filesystem;
});
}
public function register()
{
//
}
}
Here is the error I keep getting
ErrorException in FilesystemManager.php line 232:
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Filesystem\FilesystemAdapter' does not have a method 'createDriver'
in FilesystemManager.php line 232
at HandleExceptions->handleError('2', 'call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Filesystem\FilesystemAdapter' does not have a method 'createDriver'', '/home/vagrant/Code/herbals/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemManager.php', '232', array('method' => 'createDriver', 'parameters' => array(null)))
at call_user_func_array(array(object(FilesystemAdapter), 'createDriver'), array(null)) in FilesystemManager.php line 232
at FilesystemManager->__call('createDriver', array(null)) in FilesystemManager.php line 97
at FilesystemManager->createDriver(null) in FilesystemManager.php line 97
at FilesystemManager->resolve('ftp') in FilesystemManager.php line 79
at FilesystemManager->get('ftp') in FilesystemManager.php line 68
at FilesystemManager->disk('ftp') in Facade.php line 210
at Facade::__callStatic('disk', array('ftp')) in routes.php line 26
at Storage::disk('ftp') in routes.php line 26

I think you are making a little mistake here, make sure you have the ftp drivers and its details (host, username,password ) in /config/filesystem.php and reference them in your provider using $config['host'] and so on...
After playing with the api for some time , I was able to figure it out , now follow these instructions : In you /config/filesystem.php , add the new driver settings like :
'ftp' => [
'driver' => 'ftp',
'host' =>'ftp-host',
'username' => 'ftp-username',
'password' => 'ftp-pass',
'port' => 21,
'ssl' => false,
],
Next we will create an Ftp Filemanager Driver Service Provider in /app/Providers ,save the file FtpFilesystemServiceProvider.php .. Below is how mine looks like
<?php
namespace App\Providers;
use Storage;
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Ftp as FtpAdapter;
use Illuminate\Support\ServiceProvider as ServiceProvider;
class FtpFilesystemServiceProvider extends ServiceProvider {
//boot
public function boot()
{
Storage::extend('ftp',function($app, $config){
//init the client
$client = new FtpAdapter($config);
//lets now call the obj
$ftpFileSystem = new Filesystem($client);
//return
return $ftpFileSystem;
});
}//end boot
public function register()
{
//
}
}
After which we will edit /config/app.php and add the service provider to providers array so you will add the code below to the list of providers:
'App\Providers\FtpFilesystemServiceProvider',
We are done, you are off to go , yo can call the storage like this
Storage::disk("ftp");
or set ftp as your default driver , ....

Related

Laravel: Jasper reports in laravel lumen5.6

I already implemented jasper reports with laravel and it works fine for me. Now i shifted to laravel lumen for api building so i try to integrate jasper as same as i integrate in my laravel projects but in laravel lumen it throws some error as below:-
Call to undefined method Laravel\Lumen\Application::booting()
below is my connection code
class_alias(JasperPHP\JasperPHPServiceProvider::class,'JasperPHP');
$app->withFacades(); $app->withEloquent();
$app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\AuthServiceProvider::class);
$app->register(App\Providers\EventServiceProvider::class);
$app->register(JasperPHP\JasperPHPServiceProvider::class);
Please share your thoughts, Thanks in advance
I finally come up with a solution and it works fine with laravel lumen 5.6 below are the steps:-
1)Install JasperReports 6 library by below command
composer require cossou/jasperphp
In bootstrap/app.php uncomment this line $app->withFacades(); and add below code
$app->singleton('jasperphp', function ($app) {
return new JasperPHP;
});
$app->alias('JasperPHP\JasperPHPServiceProvider\JasperPHP', 'JasperPHP');
Controller part Changes
namespace App\Http\Controllers;
use JasperPHP\JasperPHP as JasperPHP;
use Illuminate\Http\Request;
//dd(__DIR__ . '/../../vendor/cossou/jasperphp/examples/hello_world.jasper');
class ReportController extends Controller {
public function generateReport() {
//JasperPHP::compile(base_path('/vendor/cossou/jasperphp/examples/hello_world.jrxml'))->execute();
$jasper = new JasperPHP;
$filename = 'gau';
$output = base_path('//public/reports/' . $filename);
$jasper->process(
base_path('/vendor/cossou/jasperphp/examples/LaravelIreporTest.jasper'),
$output,
array("pdf"),
array("test" => "Tax Invoice"),
array(
'driver' => 'mysql',
'username' => 'username',
'password' => 'password',
'host' => 'localhost',
'database' => 'database name',
'port' => '3306',
)
)->execute();
}
}

Fatal error: Uncaught Error: Class 'maimana\App' not found -- slim3

i was following alex's how to build a shopping cart lesson and everything's working fine. but then i dont know what im doing wrong so i get this error :
Fatal error: Uncaught Error: Class 'maimana\App' not found in /Applications/MAMP/htdocs/maimana/bootstrap/app.php:13 Stack trace: #0 /Applications/MAMP/htdocs/maimana/public/index.php(3): require() #1 {main} thrown in /Applications/MAMP/htdocs/maimana/bootstrap/app.php on line 13
bootsrap/app.php :
<?php
use Respect\Validation\Validator as v;
use maimana\App as MyApp;
use Slim\Views\Twig;
use Illuminate\Database\Capsule\Manager as Capsule;
session_start();
require __DIR__ . '/../vendor/autoload.php';
$app = new MyApp;
$container = $app->getContainer();
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'maimana',
'username' => 'rdp46',
'password' => 'littlelion4696',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
require __DIR__ . '/../app/routes.php';
Myapp/App.php :
namespace maimana;
use DI\ContainerBuilder;
use DI\Bridge\Slim\App as DiBridge;
class App extends DiBridge{
protected function configureContainer(ContainerBuilder $builder)
{
$builder->addDefinitions([
'settings.displayErrorDetails' => true,
]);
$builder->addDefinitions(__DIR__ . '/container.php');
}
}
anyone know what's going on?
Rename the Myapp directory to maimana (note case) and then update your composer.json to autoload the maimana namespace.
i.e. ensure that your composer.json has:
"autoload": {
"psr-4": {
"maimana\\": "maimana/"
}
}
This assumes that the maimana directoryis in the root of your project where the composer.json file is. Once you have changed composer.json, you need to run composer dumpautoload for the changes to take effect.
This is required because there is a one to one mapping between directory name that the PHP file is in and the namespace for the class in that PHP file. As the namespace in your App.php is maimana, the directory needs to be maimana.

How can I create a CRUD with Codeigniter using SQLite?

I'm starting a new project where I want to use Codeigniter with SQLite. I searched information about this and I managed to conect and get data from my SQLite database but I have no idea of how can I create a CRUD for a table with this, I'm trying to do it the same way I did in the past with MySQL buy it is not working.
Here is what I did:
config/database.php
$db['default'] = array(
'dsn' => '',
'hostname' => '',
'username' => '',
'password' => '',
'database' => APPPATH.'/database/Pasapalabra.sqlite',
'dbdriver' => 'sqlite3',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
config/autoload.php
$autoload['libraries'] = array('database');
models/modelo_prueba.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class modelo_prueba extends CI_Model {
public function getTestData() {
return $this->db->get('preguntas')->result();
}
}
?>
controllers/Welcome.php
public function __construct() {
parent::__construct();
$this->load->library("grocery_CRUD");
}
public function prueba() {
$this->load->model('modelo_prueba');
$dataSet = $this->modelo_prueba->getTestData();
$this->load->view('data_view', [
'dataSet' => $dataSet
]);
}
views/data_view.php
<h4>Lets test the data</h4>
<?php
if($dataSet) {
foreach($dataSet as $dataItem) {
echo $dataItem->definicion . "<hr>";
}
}
?>
<h5>done!</h5>
This worked for me, it shows correctly all data in 'preguntas' database, but now I'm trying to create a CRUD just like I usually do with MySQL and it's working, it shows a database error.
The only thing that I have changed from the code of above is Welcome.php
public function prueba() {
$crud = new Grocery_CRUD();
$crud->set_table('preguntas');
$output = $crud->render();
$this->load->view("example.php", $output);
}
The error that is shown in the screen is the following:
A PHP Error was encountered
Severity: Warning
Message: SQLite3::query(): Unable to prepare statement: 1, near "SHOW": syntax error
Filename: sqlite3/sqlite3_driver.php
Line Number: 129
Backtrace:
File: C:\xampp\htdocs\Pasapalabra_Isidro\application\models\Grocery_crud_model.php
Line: 436
Function: query
File: C:\xampp\htdocs\Pasapalabra_Isidro\application\libraries\Grocery_CRUD.php
Line: 48
Function: get_field_types_basic_table
File: C:\xampp\htdocs\Pasapalabra_Isidro\application\libraries\Grocery_CRUD.php
Line: 1567
Function: get_field_types
File: C:\xampp\htdocs\Pasapalabra_Isidro\application\libraries\Grocery_CRUD.php
Line: 4507
Function: showList
File: C:\xampp\htdocs\Pasapalabra_Isidro\application\controllers\Welcome.php
Line: 38
Function: render
File: C:\xampp\htdocs\Pasapalabra_Isidro\index.php
Line: 315
Function: require_once
A Database Error Occurred
Error Number: 0
not an error
SHOW COLUMNS FROM `preguntas`
Filename: C:/xampp/htdocs/Pasapalabra_Isidro/system/database/DB_driver.php
Line Number: 691
Anyone knows what is happening? There isn't any error with the database connection because the previous code worked, but it's like Grocery CRUD can't create a CRUD of an SQLite.
I'm working with Codeigniter 3.1.4 and I created the SQLite database with SQLite Manager (Firefox) 0.8.3.1
Thank you for your answers.
Hi I have found solution from grocery crud form post with works with sqlite3 you have to modify Grocery curd library first copy sqlite3 model from this link
http://www.grocerycrud.com/forums/topic/651-suport-for-sqlite3/
this model extending grocery_crud_model, just include grocery_crud_model in your sqlite model.
in grocery crud library find grocery_crud_model, where it is loading this model just replace with grocery_crud_sqlite3, and you are done. it will work perfect.
just remember you have to modify these lines manually on each update in future.

Use Illuminate database

I want to use Illuminate database(https://github.com/illuminate/database). Not with Laravel, use only in my php file.
I do
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
But it seems not working, and don't show any error message. Do I need to require any file? The illuminate directory is in the same directory with my php file.
EDIT:
I can use query now. Like this
$users = Capsule::table('users')->where('votes', '>', 100)->get();
I don't know how to use model.
User.php
class User extends Illuminate\Database\Eloquent\Model {
}
My php file
require 'vendor/autoload.php';
require 'User.php';
$users = User::where('status', '=', 1)->get();
Got error
Fatal error: Call to a member function connection() on a non-object in /Users/someone/repos/test/vendor/illuminate/database/Illuminate/Database/Eloquent/Model.php on line 2472
SOLVED:
Everything works fine. Use #majid8911 example https://github.com/mattstauffer/IlluminateNonLaravel
Thank you everyone.
take a look at here I successfully did the same with this tutorial:
https://github.com/mattstauffer/IlluminateNonLaravel

Identifier "twig" is not defined - Silex Framework

Thanks for you interest,
I have create this APP:
// web/index.php
require_once __DIR__.'/../vendor/autoload.php';
use Silex\Application;
use Silex\Provider\TwigServiceProvider;
$app = new Application();
$app['debug'] = true;
$app->register(new TwigServiceProvider(), array(
'twig.path' => __DIR__.'/templates',
'twig.class_path' => __DIR__.'/../vendor/twig/twig/lib',
'twig.options' => array('cache' => __DIR__.'/../cache'),
));
$app->get('/', function () use ($app) {
return $app['twig']->render('base.html.twig', array());
});
return $app;
But, when I load / in instance of base.html.twig be loaded, this error appear:
InvalidArgumentException: Identifier "twig" is not defined.
in /home/victor/workspace/testProject/vendor/pimple/pimple/lib/Pimple.php line 78
at Pimple->offsetGet('twig') in /home/victor/workspace/testProject/src/app.php line 41
at {closure}()
at call_user_func_array(object(Closure), array()) in /home/victor/workspace/testProject/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/HttpKernel.php line 145
at HttpKernel->handleRaw(object(Request), '1') in /home/victor/workspace/testProject/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/HttpKernel.php line 66
at HttpKernel->handle(object(Request), '1', true) in /home/victor/workspace/testProject/vendor/silex/silex/src/Silex/Application.php line 538
at Application->handle(object(Request)) in /home/victor/workspace/testProject/vendor/silex/silex/src/Silex/Application.php line 515
at Application->run() in /home/victor/workspace/testProject/web/index.php line 11
I have followed the official tutorial, installed it using composer and double checked all the paths...
Try removing the "twig.class_path" entry.
I have it like this, and it works:
$options = [
'twig.path' => ...,
'twig.options' => [
'strict_variables' => false
]
];
$this->_app->register(new TwigServiceProvider(), $options);

Categories