include external lib in laravel? - php

how to include external lib in laravel ? for example twitteroauth.php.
no need oauth other packages. because i am converting Symfony code to Laravel. Thanks.
Class 'App\Controllers\Front\TwitterOAuth' not found
.
require_once base_path().'/vendor/twitter/twitteroauth.php';
$twitterOAuth = new TwitterOAuth('app_twitter_consumer_key', 'app_twitter_consumer_secret');

Likely you have a namespace problem, Laravel is looking for the class inside the 'App\Controllers\Front' namespace.
If the class is not namespaced, use
$twitterOAuth = new \TwitterOAuth('app_twitter_consumer_key', 'app_twitter_consumer_secret');
(note the backslash before the classname)
otherwise you need to refer to its namespace, something like \Twitter\TwitterOAuth or similar, but only by looking at the class file you can tell.
You could also create an alias for the class. Inside the app\config\app.php file search for the aliases array and add your class:
'Twitter' => 'TwitterOAuth' #(or whatever namespace it's in)
By the way, why not using a specific package? Have you looked on http://packagist.org to see if there's a Twitter OAuth package already adapted for Laravel? That would make things a lot easier.

Related

'Services_Twilio' not found - Laravel

As far as I understand, I cannot simply use use Twilio to make it work. Thus, I tried require_once and require. The path should also be correct
I tried using require_once
$twilioDir = '../vendor/twilio/sdk/Services/Twilio.php';
require_once($twilioDir);
$client = new Services_Twilio($_ENV['TWILIO_ACCOUNT_SID'], $_ENV['TWILIO_AUTH_TOKEN']);
Class 'App\Http\Controllers\Services_Twilio' not found
What am I doing wrong?
Also, using require gave me error:
Cannot redeclare Services_Twilio_autoload() (previously declared in /var/www/Laravel/vendor/twilio/sdk/Services/Twilio.php:9)
I tried adding false to the line spl_autoload_register('Services_Twilio_autoload', false); in Twilio.php, but no luck
As far as I understand, I cannot simply use use Twilio to make it work.
Correct, because the class is named Services_Twilio.
use Services_Twilio; should do the trick.
Laravel handles autoloading for you. You shouldn't need to manually require the library unless Twilio has goofed something in their Composer setup.
I don't know who told you that you can't use Twilio but you can certainly use Twilio.
Grab the composer package - composer require aloha/twilio
Register the ServiceProvider in app.php like any other vendor: 'Aloha\Twilio\Support\Laravel\ServiceProvider', should be added to the providers array.
Register the facade to make life easy - in app.php add to the aliases array: 'Twilio' => 'Aloha\Twilio\Support\Laravel\Facade',
(optional) run php artisan vendor:publish so you can manage the assets that the vendor exposes to you.
Because we previously added the facade to the aliases array in our app.php we can correctly use Twilio; within our classes.
If you didn't do 3, then you need to reference the full namespace path; use Aloha\Twilio\Support\Laravel\Facade which will give you access to Twilio:: inside of that particular file.
Edit
I should note that you do not use Twilio from within the class, you must reference it outside of the class and before the class.
use Twilio; //Aloha\Twilio\Support\Laravel\Facade
class MyController {
/**
* Now you can use Twilio::whatever
*/
}
You have to import it at the top of the file.
I am not sure about the full path to the file but I am guessing it should be something like:
use Twilio\SDK\Services\Twilio
Update:
Follow the guide here:
https://github.com/aloha/laravel-twilio
The accepted answer didn't work for me.
Background: My code had worked for a while with use Services_Twilio;, but then I stopped using Twilio for months (or maybe more than a year), and then I was getting this error.
What ended up working was updating my code to say use Twilio\Rest\Client; instead and then create the client object via new Client($this->config['account_sid'], $this->config['auth_token']);.
This seems to be Twilio's newer way of doing things.
These docs helped.

(codeigniter/Neo4j) PHP : namespace and autoloader

As i'm trying to install the Neo4jPHP library in codeigniter (v2.2.x), i'm having annoying issues regarding namespaces. I've been searching for 4 hours without success.
In short, there is a libraries directory in which Neo4jPHP must be copied. So in 'libraries/', there is a directory 'Everyman/Neo4j/', which contains all the Neo4j php classes.
Also, in the same 'libraries' directory, there is a class with an autoloader function, which aims at loading the Neo4j classes (which are in 'Everyman/Neo4j/').
Inside of 'libraries'
- libraries/
|-- Everyman/
|-- Neo4j/
|-- Client.php
|-- some_other_classes.php
|-- Neo4j.php
Then, somewhere in my code, in the global namespace, i try to instantiate the class client :
$client = new Client();
But i get the error Class 'Client' not found.
In the class client, the following namespace is specified : Everyman\Neo4j.
I'm must admit that i found 2 workarounds for this issue :
From the calling code, use the fully qualified name :
new Everyman\Neo4j\Client();
Or, in Client.php, remove the namespace.
In these 2 cases, it works. However, i would like to call the Client class with these 2 condition :
1. I don't want to modify anything from the Neo4jPhp library.
2. I really don't want to have to use the fully qualified name (Everyman\Neo4j\Client). I want to use "new Client()".
Do you guys have an idea how i could achieve this (yes, i don't have a very deep understanding of namespaces, and loaders).
In Neo4j.php (file with loader)
<?php
class Everyman{
public function __construct()
{
spl_autoload_register(array($this,'autoload'));
}
public function autoload($sClass){
$sLibPath = __DIR__.DIRECTORY_SEPARATOR;
//Below, i modified the instruction so that the class file
//can be found. However, the class is not found.
$sClassFile = 'Everyman'.DIRECTORY_SEPARATOR.'Neo4j'.str_replace('\\',DIRECTORY_SEPARATOR,$sClass).'.php';
$sClassPath = $sLibPath.$sClassFile;
if (file_exists($sClassPath)) {
require($sClassPath);
}
}
}
So that's it. I think i've given you all the information i have. If no one can help me, i'll have to use 'new Everyman\Ne4j\Client();' (which works).
It may seem stupid to ask for help, as i already have found 2 workarounds, but i really want to learn how to properly handle this issue (if possible).
Thanks.
It seems code like $client = new Everyman\Neo4j\Client('localhost', 7474); is the offical usage: https://github.com/jadell/neo4jphp#connection-test
So it is not work around.
I really don't want to have to use the fully qualified name (Everyman\Neo4j\Client). I want to use "new Client()".
I don't know what you really want. But then how about putting the code below in your code which you use Everyman\Neo4j\Client:
use Everyman\Neo4j\Client;
and
$client = new Client();
See http://php.net/manual/en/language.namespaces.importing.php
The supported way of installing neo4jphp is using Composer (https://getcomposer.org/). It is a good idea to learn how to use Composer, since a large number of PHP libraries can be installed using it. It also sets up the autoloading for you, so you don't have to worry about paths and namespaces yourself.
If you don't want to write new Everyman\Neo4j\Client() every time, you can put a use statement at the top of your script: use Everyman\Neo4j\Client; and then new Client();
Thank you for your answers.
For some reason, the 'use Everyman\Neo4j\Client' command didn't work, and i decided not to perform further inquiry into this issue.
So i decided to stick with the call to '$client = new Everyman\Neo4J\Client();', instead of trying to achieve '$client = new Client();'.
Thank you for your propositions.
Loïc.

Need help referencing PECL OAuth from a Laravel 5 app

I have a Laravel 5 application where I am building a custom OAuth interface. Everything is working well, but I cannot figure out how to reference the PECL OAuth package which is installed on the server. I'm sure its something pretty simple, but I can't figure it out and Google has oddly not been very helpful.
I have this line of code:
$oauth = new OAuth($ConsumerKey, $ConsumerSecret);
When outside of Laravel, it works perfectly, referencing the PHP package. But from within Laravel, it cannot find the class - because its not part of the application.
Can anyone help?
What's the exact error PHP is sending your way? I ask because pecl extensions make functions and classes available on a PHP level -- it shouldn't matter what userland level framework (Laravel, Symfony, etc.) you're using -- so long as the same PHP binary/web-server-extension is used, you'll have access to the pecl provided functions and classes.
My guess if you're trying to use the global class OAuth from within a namespaced file.
namespace App\Some\Namespace;
//...
$object = new OAuth;
When you do this, you're telling PHP you want the class OAuth in the same namespace. i.e., the class with the full name App\Some\Namespace\OAuth. To tell PHP you want the global level class OAuth, either refer to the class name with a namespace prefix character
$object = new \OAuth;
or import the global OAuth class with the use statement.
namespace App\Some\Namespace;
use OAuth; //confusingly, the `use` statement assumes a global namespace, no prefix needed
//...
$object = new OAuth;

Symfony 2.4 How to load an external class?

I want to add an external lib into my symfony 2 project. I use the 2.4 version. I have read many "how to" but all solution doesn't work with this version.
Do you tell me how I can do to add my class in my project ?
My class Html2Pdf:
<?php
class Html2Pdf
{
// Code ...
}
?>
Do you know anything about services?
If you want to use that YoutubeDownloader class in controllers, you have to define it as a service so you can call anywhere you want.
Open your services.yml in;
YourBundle/Resources/config/services.yml
parameters:
youtubeDownload: YourBundle/YourPathToClass
services:
bundlename.controllername.controller:
class: "%youtubeDownload%"
More information:
http://symfony.com/doc/current/cookbook/controller/service.html
You can call it in a class using \Html2Pdf as you can with any none namespaced class.
Update:
As you are using Symfony and Composer the classes and namespace will already mapped so you simply need to include it using the \Html2Pdf namespace. The \ is to signify that it is a namespace based at the root level rather than a relative namespace (in the same folder).
If you were not using composer or something with an autoloader then you would need to include the file somewhere in your stack (this can be in the current file or some kind of parent file that serves this one) using include_once('**path to file**/Html2Pdf.php'). You would then use it in the same way as you would when using Symfony/Composer with the \.
This works for me.
include_once $this->get('kernel')->getRootDir() . '/../path/to/Html2Pdt.php';
$aHtml2Pdt = new \Html2Pdt();
I think this is what #Qoop is trying to say to.
I hope it helps.

require_once at symfony

I'm making now things with php + Symfony2 framework, and I have the following code:
require_once("one_file.php");
require_once("another_file.php");
... and so on.
The problem is, how to "Symfonyze" these uncomfortable require sentences, and after, how to include these files in the Symfony2 package?
We've thought about two possibilities:
Include the file at /vendors directory of symfony, or
Include each class as a service.
If these classes reside inside bundle then you could use as below:
Suppose your bundle name is AcmeDemoBundle. Place this file inside Acme/DemoBundle/Model/
//one_file.php
namespace Acme/DemoBundle/Model;
class one_file {
...........
}
To use this file inside your controller or any other file:
Here for Acme/DemoBundle/Controller/UserController.php
namespace Acme/DemoBundle/Controller
use Acme/DemoBundle/Model/one_file
class UserController {
public $one_file=new one_file();
}
In php 5.3 onwards, namespaces has been introduced. You should probably look at namespaces and its uses in php documentation
You can follow the PSR-0 standard to let the autoloader handle this. See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md and http://getcomposer.org/doc/04-schema.md#psr-0 .
Or you could keep your files as is, and tell composer to require them each time : http://getcomposer.org/doc/04-schema.md#files
You have to make a folder in your acme folder like lib puts these files in lib folder then use this statement
use Acme\DemoBundle\lib\Abc; // its your class name
You want to follow these other answers, especially the approved one, but if you are using a third party library with tons of PHP files, you can do require_once(__DIR__.'/path/to/file.php') as a quick fix.

Categories