Best Way to Add Third Party Library

Sir,

I have add a third party library in yii 2.0, in my library folder their are multiple files ,

So how can we manage this folder, we call library in the controller page, error has occur "Call to undefined method",

So please provide me the solution of this topic, how to add library in yii 2.0, how to call them(if multiple files in third party library folder), Can some other files also change like(config etc…)

Thanks

Yogendra

Please be more specific.

To use third party library in Yii2 , you have to put that folder/file in vendor folder & have to add namespace on the top of the page, & to use same in controller you have to use the particular namespace in corresponding folder.

Thanks,

Hemant

Hello,

I have a third party library and i have put that on "vendor" folder, but there are multiple files in third party library.

Should i have to include namespace in each of class file in this third party library & then how i can import that third party library in my controller?

Thanks

Yogendra

Post a link to the 3rd party library in this thread please.

You have to include namespace in each of the file in vendor folder.

Example : let your folder name is "xyz" then :

namespace vendor\xyz;

in each of file.

& in controller :

use xyz/<Your Class Name>;

Thanks,

Hemant

Hello Sam,

My exact problem is this :

I have a third party library(containing multiple files).

I want to import these library files on my controller & want to create object of one library file in same controller.

I have tried several ways , but not work.

Please tell me the best way to use third party library on my controller in Yii2.

Thanks

Yogendra

Which third party library it is? Is it under Composer control? Are classes namespaced? Is it meant to be autoloaded?

Hello Sam,

I am using the library that edit the pdf(It is external library, not yii based). There are multiple files in this library that i have put inside vendor folder & calling library in my controller page.

Library class were not namespaced , but i have added namespace in each of class file, is it right way?

It is not under Composer control(As i am understanding) & i think it is not autoloaded(As i am understanding).

Please help me to resolve this issue , it was working fine on yii1.

Thanks

Yogendra

It is a secret library? If no, please provide a link to it.

Hello Sam,

I am using fpdf/fpdi library & i have downloaded the files & code from here :

It is working fine on yii1 , but not working in yii2.

Thanks

Yogendra

Could you post code you’re writing to use it?

Hello Sam,

My code as follows :

At the top of controller class :

            use app&#092;readpdfandupdatelib&#092;fpdf;


            use app&#092;readpdfandupdatelib&#092;fpdi;

& in controller action :

            &#036;pdf = new FPDI();		


	&#036;pageCount = &#036;pdf-&gt;setSourceFile(&quot;C:/xampp/htdocs/basic/mmc_postcard.pdf&quot;);


	&#036;folder = (&quot;C:/xampp/htdocs/basic/pdf&quot;); 


	&#036;tplIdx = &#036;pdf-&gt;importPage(1);


	&#036;pdf-&gt;AddPage('L',array(608,926));


	&#036;pdf-&gt;useTemplate(&#036;tplIdx, 0, 0, 0, false);


	&#036;pdf-&gt;SetAutoPageBreak(true,-2);


	&#036;tplIdx = &#036;pdf-&gt;importPage(2);


	&#036;pdf-&gt;AddPage('L',array(608,926));


	&#036;pdf-&gt;SetAutoPageBreak(true,-2);


	&#036;pdf-&gt;useTemplate(&#036;tplIdx, 0, 0, 0, false);


	&#036;pdf-&gt;SetXY(740, 471);


	&#036;pdf-&gt;SetFont('Arial','B',55);


	&#036;pdf-&gt;SetTextColor(255,255,255);


        &#036;pdf-&gt;Cell(10,20,&quot;yogendra&quot;);


	&#036;pdf-&gt;Output(&quot;&#036;folder&quot;.&quot;100&quot;.&quot;.pdf&quot;);

Thanks

Yogendra

And the full error text including stacktrace please.

Hi Sam,

"Division by zero" error is coming in yii2 , while in yii1 it was working perfectly.

I have included "namespace" in each of library file & instead of "require" i have used "use" property of namespace.

Following is the full error text including stacktrace(also attached) :

Error

PHP Warning – yii\base\ErrorException

Division by zero

1. in C:&#092;xampp&#092;htdocs&#092;yii2&#092;readpdfandupdatelib&#092;fpdi_pdf_parser.php at line 225


216217218219220221222223224225226227228229230231232233234





 


            if (&#33;is_null(&#036;box) &amp;&amp; &#036;box[0] == pdf_parser::TYPE_OBJREF) {


                &#036;tmp_box = &#036;this-&gt;resolveObject(&#036;box);


                &#036;box = &#036;tmp_box[1];


            }


 


            if (&#33;is_null(&#036;box) &amp;&amp; &#036;box[0] == pdf_parser::TYPE_ARRAY) {


                &#036;b = &#036;box[1];


                return array(


                    'x' =&gt; &#036;b[0][1] / &#036;k,


                    'y' =&gt; &#036;b[1][1] / &#036;k,


                    'w' =&gt; abs(&#036;b[0][1] - &#036;b[2][1]) / &#036;k,


                    'h' =&gt; abs(&#036;b[1][1] - &#036;b[3][1]) / &#036;k,


                    'llx' =&gt; min(&#036;b[0][1], &#036;b[2][1]) / &#036;k,


                    'lly' =&gt; min(&#036;b[1][1], &#036;b[3][1]) / &#036;k,


                    'urx' =&gt; max(&#036;b[0][1], &#036;b[2][1]) / &#036;k,


                    'ury' =&gt; max(&#036;b[1][1], &#036;b[3][1]) / &#036;k,					


                );


            } else if (&#33;isset(&#036;page[1][1]['/Parent'])) {





2. in C:&#092;xampp&#092;htdocs&#092;yii2&#092;readpdfandupdatelib&#092;fpdi_pdf_parser.php – yii&#092;base&#092;ErrorHandler::handleError(2, 'Division by zero', 'C:&#092;xampp&#092;htdocs&#092;yii2&#092;readpdfandu...', 225, ...) at line 225


219220221222223224225226227228229230231





                &#036;box = &#036;tmp_box[1];


            }


 


            if (&#33;is_null(&#036;box) &amp;&amp; &#036;box[0] == pdf_parser::TYPE_ARRAY) {


                &#036;b = &#036;box[1];


                return array(


                    'x' =&gt; &#036;b[0][1] / &#036;k,


                    'y' =&gt; &#036;b[1][1] / &#036;k,


                    'w' =&gt; abs(&#036;b[0][1] - &#036;b[2][1]) / &#036;k,


                    'h' =&gt; abs(&#036;b[1][1] - &#036;b[3][1]) / &#036;k,


                    'llx' =&gt; min(&#036;b[0][1], &#036;b[2][1]) / &#036;k,


                    'lly' =&gt; min(&#036;b[1][1], &#036;b[3][1]) / &#036;k,


                    'urx' =&gt; max(&#036;b[0][1], &#036;b[2][1]) / &#036;k,





3. in C:&#092;xampp&#092;htdocs&#092;yii2&#092;readpdfandupdatelib&#092;fpdi_pdf_parser.php – app&#092;readpdfandupdatelib&#092;fpdi_pdf_parser::_getPageBox([0 =&gt; 9, 'obj' =&gt; 5, 'gen' =&gt; 0, 1 =&gt; [5, ['/ArtBox' =&gt; [6, [[12, 0], [12, 0], [12, 612.24], [12, 858.96]]], '/Contents' =&gt; [8, 6, 0], '/MediaBox' =&gt; [6, [[12, 0], [12, 0], [12, 612.24], [12, 858.96]]], '/Parent' =&gt; [8, 3, 0], ...]]], '/MediaBox', null) at line 271


265266267268269270271272273274275276277





     */


    protected function _getPageBoxes(&#036;page, &#036;k)


    {


        &#036;boxes = array();


 


        foreach(&#036;this-&gt;availableBoxes AS &#036;box) {


            if (&#036;_box = &#036;this-&gt;_getPageBox(&#036;page, &#036;box, &#036;k)) {


                &#036;boxes[&#036;box] = &#036;_box;


            }


        }


 


        return &#036;boxes;


    }





4. in C:&#092;xampp&#092;htdocs&#092;yii2&#092;readpdfandupdatelib&#092;fpdi_pdf_parser.php – app&#092;readpdfandupdatelib&#092;fpdi_pdf_parser::_getPageBoxes([0 =&gt; 9, 'obj' =&gt; 5, 'gen' =&gt; 0, 1 =&gt; [5, ['/ArtBox' =&gt; [6, [[12, 0], [12, 0], [12, 612.24], [12, 858.96]]], '/Contents' =&gt; [8, 6, 0], '/MediaBox' =&gt; [6, [[12, 0], [12, 0], [12, 612.24], [12, 858.96]]], '/Parent' =&gt; [8, 3, 0], ...]]], null) at line 256


250251252253254255256257258259260261262





    public function getPageBoxes(&#036;pageNo, &#036;k)


    {


        if (&#33;isset(&#036;this-&gt;_pages[&#036;pageNo - 1])) {


            throw new InvalidArgumentException('Page ' . &#036;pageNo . ' does not exists.');


        }


 


        return &#036;this-&gt;_getPageBoxes(&#036;this-&gt;_pages[&#036;pageNo - 1], &#036;k);


    }


 


    /**


     * Get all boxes from /Page dictionary


     *


     * @param array &#036;page A /Page dictionary





5. in C:&#092;xampp&#092;htdocs&#092;yii2&#092;readpdfandupdatelib&#092;fpdi.php – app&#092;readpdfandupdatelib&#092;fpdi_pdf_parser::getPageBoxes(1, null) at line 198


192193194195196197198199200201202203204





        &#036;parser-&gt;setPageNo(&#036;pageNo);


 


        if (&#33;in_array(&#036;boxName, &#036;parser-&gt;availableBoxes)) {


            throw new InvalidArgumentException(sprintf('Unknown box: %s', &#036;boxName));


        }


 


        &#036;pageBoxes = &#036;parser-&gt;getPageBoxes(&#036;pageNo, &#036;this-&gt;k);


 


        /**


         * MediaBox


         * CropBox: Default -&gt; MediaBox


         * BleedBox: Default -&gt; CropBox


         * TrimBox: Default -&gt; CropBox





6. in C:&#092;xampp&#092;htdocs&#092;yii2&#092;controllers&#092;SiteController.php – app&#092;readpdfandupdatelib&#092;FPDI::importPage(1) at line 70


64656667686970717273747576





        &#036;this-&gt;layout = false;					


        //require_once(Yii::&#036;app-&gt;basePath.&quot;/readpdfandupdatelib/fpdf.php&quot;);


        //require_once(Yii::&#036;app-&gt;basePath.&quot;/readpdfandupdatelib/fpdi.php&quot;);


        &#036;pdf = new pdffolder&#092;FPDI();


        &#036;pageCount = &#036;pdf-&gt;setSourceFile(Yii::&#036;app-&gt;basePath.&quot;/mmc_flyer.pdf&quot;);


        &#036;folder = Yii::&#036;app-&gt;basePath . &quot;/pdf/&quot;;		


        &#036;tplIdx = &#036;pdf-&gt;importPage(1);


        &#036;pdf-&gt;AddPage('P','A4');


        &#036;pdf-&gt;useTemplate(&#036;tplIdx, 0, 0, 215, false);


        &#036;pdf-&gt;SetAutoPageBreak(true,1);


        &#036;pdf-&gt;SetFont('Arial','B',18);


        &#036;pdf-&gt;SetXY(28, 276);


        &#036;pdf-&gt;SetTextColor(255,255,255);





7. app&#092;controllers&#092;SiteController::actionNewflyer()


8. in C:&#092;xampp&#092;htdocs&#092;yii2&#092;vendor&#092;yiisoft&#092;yii2&#092;base&#092;InlineAction.php – call_user_func_array([app&#092;controllers&#092;SiteController, 'actionNewflyer'], []) at line 55


9. in C:&#092;xampp&#092;htdocs&#092;yii2&#092;vendor&#092;yiisoft&#092;yii2&#092;base&#092;Controller.php – yii&#092;base&#092;InlineAction::runWithParams([]) at line 151


10. in C:&#092;xampp&#092;htdocs&#092;yii2&#092;vendor&#092;yiisoft&#092;yii2&#092;base&#092;Module.php – yii&#092;base&#092;Controller::runAction('newflyer', []) at line 455


11. in C:&#092;xampp&#092;htdocs&#092;yii2&#092;vendor&#092;yiisoft&#092;yii2&#092;web&#092;Application.php – yii&#092;base&#092;Module::runAction('site/newflyer', []) at line 83


12. in C:&#092;xampp&#092;htdocs&#092;yii2&#092;vendor&#092;yiisoft&#092;yii2&#092;base&#092;Application.php – yii&#092;web&#092;Application::handleRequest(yii&#092;web&#092;Request) at line 375


13. in C:&#092;xampp&#092;htdocs&#092;yii2&#092;web&#092;index.php – yii&#092;base&#092;Application::run() at line 12


6789101112





 


require(__DIR__ . '/&#46;&#46;/vendor/autoload.php');


require(__DIR__ . '/&#46;&#46;/vendor/yiisoft/yii2/Yii.php');


 


&#036;config = require(__DIR__ . '/&#46;&#46;/config/web.php');


 


(new yii&#092;web&#092;Application(&#036;config))-&gt;run();

$_SERVER = [

'REDIRECT_MIBDIRS' =&gt; 'C:/xampp/php/extras/mibs',


'REDIRECT_MYSQL_HOME' =&gt; '&#092;&#092;xampp&#092;&#092;mysql&#092;&#092;bin',


'REDIRECT_OPENSSL_CONF' =&gt; 'C:/xampp/apache/bin/openssl.cnf',


'REDIRECT_PHP_PEAR_SYSCONF_DIR' =&gt; '&#092;&#092;xampp&#092;&#092;php',


'REDIRECT_PHPRC' =&gt; '&#092;&#092;xampp&#092;&#092;php',


'REDIRECT_TMP' =&gt; '&#092;&#092;xampp&#092;&#092;tmp',


'REDIRECT_STATUS' =&gt; '200',


'MIBDIRS' =&gt; 'C:/xampp/php/extras/mibs',


'MYSQL_HOME' =&gt; '&#092;&#092;xampp&#092;&#092;mysql&#092;&#092;bin',


'OPENSSL_CONF' =&gt; 'C:/xampp/apache/bin/openssl.cnf',


'PHP_PEAR_SYSCONF_DIR' =&gt; '&#092;&#092;xampp&#092;&#092;php',


'PHPRC' =&gt; '&#092;&#092;xampp&#092;&#092;php',


'TMP' =&gt; '&#092;&#092;xampp&#092;&#092;tmp',


'HTTP_HOST' =&gt; 'mmcfrontend.com',


'HTTP_USER_AGENT' =&gt; 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0',


'HTTP_ACCEPT' =&gt; 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',


'HTTP_ACCEPT_LANGUAGE' =&gt; 'en-US,en;q=0.5',


'HTTP_ACCEPT_ENCODING' =&gt; 'gzip, deflate',


'HTTP_REFERER' =&gt; 'http://mmcfrontend.com/site/flyer',


'HTTP_CONNECTION' =&gt; 'keep-alive',


'PATH' =&gt; 'C:&#092;&#092;Program Files (x86)&#092;&#092;EasyPHP-DevServer-14.1VC11&#092;&#092;binaries&#092;&#092;php&#092;&#092;php_runningversion;C:&#092;&#092;Windows&#092;&#092;system32;C:&#092;&#092;Windows;C:&#092;&#092;Windows&#092;&#092;System32&#092;&#092;Wbem;C:&#092;&#092;Windows&#092;&#092;System32&#092;&#092;WindowsPowerShell&#092;&#092;v1.0&#092;&#092;;C:&#092;&#092;Program Files&#092;&#092;Intel&#092;&#092;DMIX;C:&#092;&#092;xampp&#092;&#092;php;C:&#092;&#092;ProgramData&#092;&#092;ComposerSetup&#092;&#092;bin',


'SystemRoot' =&gt; 'C:&#092;&#092;Windows',


'COMSPEC' =&gt; 'C:&#092;&#092;Windows&#092;&#092;system32&#092;&#092;cmd.exe',


'PATHEXT' =&gt; '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC',


'WINDIR' =&gt; 'C:&#092;&#092;Windows',


'SERVER_SIGNATURE' =&gt; '

Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.6.3 Server at mmcfrontend.com Port 80

',

'SERVER_SOFTWARE' =&gt; 'Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.6.3',


'SERVER_NAME' =&gt; 'mmcfrontend.com',


'SERVER_ADDR' =&gt; '127.0.0.1',


'SERVER_PORT' =&gt; '80',


'REMOTE_ADDR' =&gt; '127.0.0.1',


'DOCUMENT_ROOT' =&gt; 'C:/xampp/htdocs/yii2/web',


'REQUEST_SCHEME' =&gt; 'http',


'CONTEXT_PREFIX' =&gt; '',


'CONTEXT_DOCUMENT_ROOT' =&gt; 'C:/xampp/htdocs/yii2/web',


'SERVER_ADMIN' =&gt; 'webmaster@dummy-host2.localhost',


'SCRIPT_FILENAME' =&gt; 'C:/xampp/htdocs/yii2/web/index.php',


'REMOTE_PORT' =&gt; '53313',


'REDIRECT_URL' =&gt; '/site/newflyer',


'GATEWAY_INTERFACE' =&gt; 'CGI/1.1',


'SERVER_PROTOCOL' =&gt; 'HTTP/1.1',


'REQUEST_METHOD' =&gt; 'GET',


'QUERY_STRING' =&gt; '',


'REQUEST_URI' =&gt; '/site/newflyer',


'SCRIPT_NAME' =&gt; '/index.php',


'PHP_SELF' =&gt; '/index.php',


'REQUEST_TIME_FLOAT' =&gt; 1421736797.921000003814697265625,


'REQUEST_TIME' =&gt; 1421736797,

];

Yii Framework

2015-01-20, 07:53:18

Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.6.3

Yii Framework/2.0.1

Well, “Division by zero” isn’t connected with namespaces or autoloading. It is library problem, not framework issue.

Hi Sam,

This error is not coming in Normal PHP & Yii1 , i am using same on yii2.

I think there is a problem of something not including properly may be namespace or require file.

Please give me answer of following 2 query , so that i can resolve the solution :

  1. If i have to add any third party library then should i have to add "namespace" in each of library class file?

  2. In library file if any where “require” including for example, “require_once(‘fpdf.php’)” , should i have to replace that with “use app\readpdfandupdatelib\fpdf”?

Thanks

Yogendra

Most probably you’re silencing errors in environment where 1.1 is used. 2.0 doesn’t tolerate libraries that are firing notices and warnings.

  1. No.

  2. No.

Hi Sam,

Thanks for your response , please tell me the last thing :

I have just include 2 files & create 1 object , like :

require_once(Yii::$app->basePath."/readpdfandupdatelib/fpdf.php");

require_once(Yii::$app->basePath."/readpdfandupdatelib/fpdi.php");

$pdf = new FPDI();

When i am doing so, error “Class ‘app\controllers\FPDI’ not found” is comming.

& when i use "namespace" & further follow the next errors , and manage name space then "Division by zero" error comes.

Please tell me the way that i can include "fpdf.php" & "fpdi.php" on my controller & create "FPDI" object!

Thanks

Yogendra