I am trying to use core php library in codeigniter. After inculding files in php code, It contain code as:
use Mylib\Configuration\GenericConfiguration;
use Mylib\Operations\Search;
I am getting error on using 'use' keyword and I am not able to find how to use this files in codeigniter code. So, Please help me to find solution for this problem.
Thanks in advance.
You can use CodeIgniter libraries that might help you to avoid use keyword
In CodeIgniter, libraries folder is available in
[CI_ROOT]/application/libraries/
For this simply place your all Mylib\Configuration\GenericConfiguration, Mylib\Operations\Search class files into the libraries folder
Then, you can mention these libraries in autoload.php ([CI_ROOT]/application/config/autoload.php) as
$autoload['libraries'] = array('database','session','[Your_Library_Class Name_In_Small_Letters]');
or, if you want to use in a specific controller or in a specific method, use like
$this->load->library('[Your_Library_Class_Name_In_Small_Letters]');
after doing this, you can call your library functions with its instance like
$this->[Your_Library_Class Name_In_Small_Letters]->libraryFunctionName();
See Codeigniter documentation for brief explanation.
Related
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.
I'm trying to use a php library called tcpdf-extension. This library uses namespaces and 'use' commmands. The rest of my code does not. I'm pretty new to namespaces and do not understand how to use them.
If I include the library, I get problems with php not being able to find other included/required files. For instance: 'PHP Fatal error: require_once(): Failed opening required <filepath>(include_path='.;C:\php\pear') in <filepath> on line 29'
Another issue is some of my previously working website pages just hang without any error message. If I remove the include for the library, everything goes back to normal.
If I use the library in a separate page from the rest of my code, then there's no problems, but as soon as I include it in any other page, it fails.
Why is this happening and what can I do to fix it?
Update: example of including the library:
require_once ('tcpdf/Extension/Helper.php');
require_once ('tcpdf/Extension/Table/Table.php');
require_once ('tcpdf/Extension/Table/Cell.php');
require_once ('tcpdf/Extension/Table/Row.php');
require_once ('tcpdf/Extension/Table/TableConverter.php');
require_once ('tcpdf\Extension\Attribute\AbstractAttribute.php');
require_once ('tcpdf\Extension\Attribute\BackgroundAttribute.php');
require_once ('tcpdf\Extension\Attribute\BackgroundFormatterOptions.php');
if this is commented out, the problems go away, but I need to use this library.
Here's a section from Table.php:
<?php
namespace Tcpdf\Extension\Table;
class Table{
...
Here's a section from Cell.php:
namespace Tcpdf\Extension\Table;
use Tcpdf\Extension\Attribute\BackgroundAttribute;
class Cell
{
...
My guess is that the use and/or namespace commands have something to do with it because that's the only thing about this library that is different than other libraries I've used without any problems. I've also tried commenting out the use commands and that makes code outside the library work okay, but it makes the library not work. Perhaps after I include this library I need to give another 'use' command to get back to the right namespace for the rest of my code. However, since I've never set a namespace for the code, I don't know what the use command would be.
This is the library in question:
https://github.com/naitsirch/tcpdf-extension
Another detail that might be relevant: Most of my code is procedural style. I often make use of classes, but most of this (very large) code base is not in a class at all. If it were all in classes, I'm sure I could add use statements for each class, but that is not the case.
You should use "use" operator in your file by below way.
use Tcpdf\Extension\Table\Table as TableClass;
after this, "new TableClass();" would instantiate a Tcpdf\Extension\Table\Table Class
For more info, check below link:-
PHP namespaces and "use"
Hope it will help you :-)
The only way I have found to use this library with my existing, non-namespaced, procedural code is to remove all use statements and namespace statements from the library. This library only has 8 files, so this wasn't hard. I'd still like to hear answers from people if they have a better approach, as I'm sure this issue will come up again.
I am new to cakephp and I managed to work my way out doing some great stuff with this powerful framewoek. Seeking some organization in my code, I am looking for a way to create some kind of side library to call it whenever it.
I am working on the mailing layer of my application using the built in mailing utility, so I want to know how to create a library where i can store my functions.
Thank you.
you can create your own library file in
/app/Lib/Library_Class_name.php
class Library_Class_name {
public function function_name() {
}
}
after this you should need to use load this Library file to your application use below code
App::uses('Library_Class_name', 'Lib');
One this, If you want to load this file for entire application you need to use above App::uses code to your Config/bootstrap.php file otherwise you can use this to needed files or functions.
I am learning how to use the Zend Framework. I come from a codeigniter background.
What I want to do is define a function somewhere that performs a very simple yet useful function. I am predominantly going to use the function within view scripts. I don;t really want to make a whole class for such a simple thing, so my question is, is there anywhere were can I put a file containg all of my general functions and how do I go about using it?
Thanks
John
What you are looking for are view helpers.
A view helper however is a function in a helper class. Therefore only one view helper can be put in a single class.
If you are using the project setup as used in the quick start tutorial or as generated by Zend_Tool, your view helpers should be put in the application/views/helpers directory.
Declaring a view helper is pretty simple, and is explained in great detail on this page of the zend framework documentation (i must say it's a bit hidden in the docs):
http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.custom
Some background information on view helpers as well as some standard included ones can be found on this page: http://framework.zend.com/manual/en/zend.view.helpers.html
Hope this helped you in the right direction.
If you realy whant to use a function you can make a library class with a static method , make a folder like this Application/Library/MyLib , then at bootstrap register MyLib namespace like this
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('MyLib'); , then inside MyLib folder you can make a filename MyClass , with a class name MyLib_MyClass , then inside you're view you can call MyLib_MyClass::staticMethod().
Tough i suggest you make a view helper for this . You don't realy use functions in ZF like you where used to in CI ( i was in you're exact situation a few months ago ) , ZF is all about OOP .
Just starting to use CodeIgniter, and I'd like to import some of my old classes for use in a new project. However, I don't want to modify them too much to fit into the CI way of doing things, and I'd like to be able to continue to use NetBeans' autocomplete functionality, which doesn't work too well with CI.
So, what is the best way to load custom classes & class files into CodeIgniter without having to use the library/model loading mechanisms?
I apologise if this is something I should be able to find quickly, but I can't seem to find what I'm after. Everything I see is just telling me how to go through CI.
To do it codeigniter way, place your custom classes in libraries folder of codeigniter. And then use it by adding that class as library in your controller like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Someclass {
public function some_function()
{
}
}
/* End of file Someclass.php */
using in controller:
$this->load->library('someclass');
checkout complete article at http://www.codeigniter.com/user_guide/general/creating_libraries.html
Libraries are easy to write but they have a few restrictions. Constructors can only take an array as a parameter and it's assumed that only one class will exist per file.
You can include any of your own classes to work with them however you want, as this is only PHP ofc :)
include APPPATH . 'classes/foo.php';
$foo = new Foo;
Or set up an __autoload() function in your config.php (best place for it to be) and you can have access to your classes without having to include them.
I'd say you at least write a wrapper class that could require the classes and instantiate the objects and make them accessible. Then you could probably autoload such library and use it as needed.
I would recommend that you at least tried to have them fit in the CI way, as moving forward this will make you life much more easy. I've been in kind of the same position and learned just this along the way.
require_once(PHYSICAL_BASE_URL . 'system/application/controllers/abc.php');
$report= new abc();
Next use the function detail in abc contoller:
$mark=$report->detail($user);
If you're just starting to use CodeIgniter, maybe you ought to check Kohana (http://kohanaframework.org/). It is very similar to CodeIgniter in many ways but it loads classes in the normal way (using new ClassName()) so Netbeans' autocompletion features should works normally.