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.
Related
I found, that in Yii 2 there is an interesting namespace app. For example,
use app\assets\AppAsset;
There http://www.yiiframework.com/doc-2.0/guide-concept-autoloading.html I found info
When using the Basic Project Template, you may put your classes under
the top-level namespace app so that they can be autoloaded by Yii
without the need of defining a new alias. This is because #app is a
predefined alias, and a class name like app\components\MyClass can be
resolved into the class file AppBasePath/components/MyClass.php,
according to the algorithm just described.
Also I found http://www.yiiframework.com/doc-2.0/guide-concept-aliases.html#predefined-aliases
#app, the base path of the currently running application.
And, when I call
print_r(Yii::getAlias("#app"));die;
it's printing
/home/u1326jyq/domains/lifesim.biz/public_html/dev
Am I right or not, that namespace app is equal to /home/u1326jyq/domains/lifesim.biz/public_html/dev ?
But, if I trying to execute script like
use home\u1326jyq\domains\lifesim.biz\public_html\dev\assets\syntaxhighlighter\SyntaxhighlighterAsset;
SyntaxhighlighterAsset::register($this);
it's fails with 500 error.
Where is definition of the app namespace in Yii2 and how to define custom myCustomApp namespace, that will be equal to my path /home/u1326jyq/domains/lifesim.biz/public_html/dev, to using it with code like
use myCustomApp\assets\syntaxhighlighter\SyntaxhighlighterAsset;
that must be equal to code
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__.'/assets/syntaxhighlighter/SyntaxhighlighterAsset');
?
Thank you very much!
Inside my Controller I want a function to use mpdf e.g.
public function actionPdf(){
include("MPDF57/mpdf.php");
$mpdf=new mPDF('c');
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML("<h1>Hello World!</h1>");
$mpdf->Output('filename.pdf', 'F');
}
}
This does not work, and throws an error:
Class 'app\controllers\mPDF' not found
What should I do If I want to autoload the class
(a). Just for this Controller Action
(b). To make it usable everywhere just by using the use statement.
I know it has to do something with namespaces but don't know how do I define a namespace, and where do I place this MPDF57 folder and then make it accessible.
I also tried this :
$name = "MPDF57/mpdf.php";
spl_autoload_register(function ($name) {
var_dump($name);
});
But this didn't work either. throws the same error when I call my controller Action.
Here is the namespace declaration and use statements inside :
namespace app\controllers;
use Yii;
use app\models\Regs;
use app\models\Voters;
use app\models\RegsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use \yii\web\Response;
use yii\helpers\Html;
use kartik\mpdf\Pdf;
Yii has already had autoloader, you do need nothing to load your class.
Just create your class with correct namespace and it will be loaded where are you using it only.
Namspace should represent real path to PHP file. PHP file name and class name should be same.
You should simply use mpdf/mpdf package :
Install it using composer :
composer require "mpdf/mpdf" ">=6.0.0"
Use it like this :
$mpdf = new \mPDF();
Or you can use a yii2 extension like this one : https://github.com/kartik-v/yii2-mpdf
I've faced such problems in one of my previous projects. I'm not good at PHP or Yii2 - so follow my guide on your own risk :)
When you you add use path\to\ExternalLibrary that means the interface is ready to use inside current class (e.g. CurrentController.php).
That means your application knows how to bring your path to it's stage.
E.g. use common\models\Post lets you directly to use Post class, as $posts = new Post;
So if your library contains only one file, just put is some "canonic" path. To common\models\ for example. So you can use it like any other model interface.
But for sake of your project put it on vendor folder. Then install a random library with composer. And observe which files are modified (1-3 generally). Also try to understand the modification logic. When you get sure that you've grasped everything, copy and paste these parts and change the paths, names, etc. for your library.
The best way, I think, is to make your library PSR-4 compatible and ship it as a PHP package. Thus, others can also benefit from your work.
There are lots of guides about making php packages.
http://sitepoint.com/starting-new-php-package-right-way/
https://knpuniversity.com/screencast/question-answer-day/create-composer-package
http://jessesnet.com/development-notes/2015/create-php-composer-package/
http://culttt.com/2014/03/12/build-php-package/
If you are planning to be a good PHP developer, I recommend to look up Josh Lockhart's "Modern PHP: New Features and Good Practices" book ( free pdfs are available :) ). That will help you to understand the fundamentals of OO PHP including namespaces, interfaces etc. So, you will be able to handle such problems in modern way.
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.
I have an issue on Zf2 when i try to create an xml document, using :
$config = new Zend\Config\Config(array(), true);
i followed the official documentation here :
Zend framework 2 Zend\Config\writer
I'm sure zf2 is loader and i don't understand what happening there but.
My output is
Fatal error: Class 'XmlGenerator\Controller\Zend\Config\Config' not found in C:\wamp\www\myLink\module\XmlGenerator\src\XmlGenerator\Controller\XmlGeneratorController.php
if someone can explain please, i'm lost ! :p
Thanks in advance !!
You are having a namespacing problem.
You need to either include the Zend namespace with use Zend; after where you declare the name space or change your code to $config = new \Zend\Config\Config(array(), true);
PHP is looking for class in your current namespace so it is adding the current namespace to fill name of the class (in this case "XmlGenerator\Controller") which the autoloader uses to determine which directory to get the class from. Since the Zend code isn't in the same directory as your controller, the autoloader chokes and gives you the error.
You need to tell your code that you are also using the Zend namespace (via use) or that the class you are using is in the global namespace (prepending the \ to the class name)
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.