I've been researching about this for hours, but in vain... I have a Zend application and these are two classes from it:
class Application_Form_Disciplines extends Zend_Form {
public function init() {
$this->addElementPrefixPath('My_Validate', '../library/validate', 'validate');
...
$credits->addValidator('My_Validate_NumericBetween');
...
}
}
class My_Validate_NumericBetween extends Zend_Validate_Abstract
{
...
}
The problem is that when i submit the form i get "Plugin by name 'My_Validate_NumericBetween' was not found in the registry; used paths: My_Validate_: ../library/validate/ Zend_Validate_: Zend/Validate/". The class named "My_Validate_NumericBetween" is found in project/library/validate. I've tried a lot of things found on the net, but nothing worked.
Thank you!
Try:
$this->addElementPrefixPath('My_Validate', APPLICATION_PATH . '/../library/validate', 'validate');
$credits->addValidator('NumericBetween');
Side note: Typical PSR-0 classname/file scheme would be to have the class My_Validate_NumericBetween stored in the file library/My/Validate/NumericBetween.php, rather than library/validate/NumericBetween.php (which I infer you are using).
Put this in Bootstrap.php, it will save you some typing in the future too
Zend_Loader_Autoloader::getInstance()->registerNamespace('My_');
rename the file to NumericBetween.php
the class name should remain My_Validate_NumericBetween
Related
So subject is the question. Yes, I've searched this forum and googled too. All I've got - useless Symfony docs and casts, some general advises, cli-s and nothing case specific. Maybe yahoo or duckduck could help better?
Everyone is talking about bundles, about how it is important to create them, probably because under the hood Symfony is pushing users away from custom libraries, but no one is actually explains how to start using a bundle - how to start calling its methods.
No, my library is not a composer or whatever package. No, library methods do not return Response objects. No, I am not dealing with composer or recompilations or cli (I use Composercat). No, I will not put library to github or packagist to load it via composer or whatever because it is private library.
Sorry about emotional off-topic.
About the case: I've put my library into the folder
src/lib/MyLibrary.php
I suspect that library class is autoloaded, because if I do not extend Controller with it (if I declare class MyLibrary instead of class MyLibrary extends Controller) - Symfony spits "class name in use" error.
So question: in my controller how to call library method?
$this->get('MyLibrary') doesn't work.
echo print_r($this) doesn't show MyLibrary in this registry too.
Looks like library file is loaded but not registered and/or instantiated. If it is so, then where to point Symfony to register it?
So most of this question is really about how php manages classes. Not so much about Symfony. But that's okay.
To start with it would be best to move project/src/lib to just project/lib. Symfony has some scanning stuff going on in the src directory and you really don't want to have your library mixed up in it.
So:
# lib/MyLibrary.php
namespace Roman;
class MyLibrary
{
public function hello()
{
return 'hello';
}
}
Notice that I added a namespace (Roman) just to distinguish your code from Symfony's.
Now you need to tweak composer.json in order to allow php to autoload your classes:
"autoload": {
"psr-4": {
"App\\": "src/",
"Roman\\": "lib/"
}
},
After adding the Roman line, run "composer dump-autoload" to regenerate the autoload files.
After that, it's just a question of using regular php inside of your application:
# src/Controller/DefaultController.php
namespace App\Controller;
use Roman\MyLibrary;
use Symfony\Component\HttpFoundation\Response;
class DefaultController
{
public function index()
{
$myLibrary = new MyLibrary();
$hello = $myLibrary->hello();
return new Response($hello);
}
}
And that should get your started.
I have created a model Admin_Model under Application\core directory of code igniter. I put all basic database operations under it. When I try to extend my models who are under Application\model directory, it throws error.
Fatal error: Class 'Admin_Model' not found in <path to root>/application/models/new_model.php on line 3
Should I miss any configuration?
Thanks all for your efforts.
I find a solution.
By default CodeIgniter have a setting in its config file.
$config['subclass_prefix'] = 'MY_';
I just replace 'MY_' with 'Admin_' everything works fine.
$config['subclass_prefix'] = 'Admin_';
More appropriate solution is
Put class file in libraries folder
Add following code to config.php
function __autoload($classname)
{
if(strpos($classname,'CI_') == 0)
{
$file = APPPATH.'libraries/'.$classname.'.php';
if(file_exists($file))
{
#include_once($file);
}
}
}
That's All
This may also be helpful, if you still have problems with __autoload to use class from libraries. it's work for me, when I use class library tobe parent class at controller.
PHP 5 >= 5.1.2, PHP 7
this is mycode at application/config.php
spl_autoload_register(function ($classname){
if(strpos($classname,'CI_') == 0){
$file = APPPATH.'libraries/'.$classname.'.php';
if(file_exists($file)){
#include_once($file);
}
}
});
Extending Core Class
If all you need to do is add some functionality to an existing library - perhaps add a function or two - then it's overkill to replace the entire library with your version. In this case it's better to simply extend the class. Extending a class is nearly identical to replacing a class with a couple exceptions:
The class declaration must extend the parent class.
Your new class name and filename must be prefixed with MY_ (this item is configurable. See below.).
For example, to extend the native Model class you'll create a file named application/core/MY_Model.php, and declare your class with:
class MY_Model extends CI_Model {
}
Note: If you need to use a constructor in your class make sure you extend the parent constructor:
class MY_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
}
I setup my environment on a Windows machine. Then after move to production using Ubuntu, I had the class not found error.
I discovered my files extension were PHP (capital letter). Linux is case sensitive, so the file name need to be lowercase php.
I am trying to learn how to use the Zend Framework and ive ran into trouble. Im trying to place the current users name in the header of the application (displayed on every page), specifically /layouts/scripts/default.phtml.
The MVC architecture is very new to me and confusing me greatly. I do not want to have to place the logic to display this username in the controller every time (this is probably the wrong way to do it anyway), so where would I place the code to assign this variable if not in each controller?
Cheers
This is the kind of thing that action helpers were designed for. A full tutorial on them is a bit beyond the scope of SO, but there are several good tutorials available.
Start with the Zend Framework Documentation and then take a look at Mathew Weier O'Phinney's tutorial and also this one by Rob Allen.
The issue with using a base controller for this kind of thing is that the resources are loaded regardless of wether your controller needs them or not, whereas action helpers are loaded only if needed.
I almost forgot the excellent ZendCasts have a video on action helpers.
You want a base controller and to assign that in the preDispatch method:
class MyApp_Controller_Action extends Zend_Controller_Action {
public function preDispatch() {
parent::preDispatch();
Zend_Layout::getMvcInstance()->assign('username', getCurrentUserName());
}
}
Then extend your own controllers with that new class:
class MyApp_Module_ActionController extends MyApp_Controller_Action {
}
Then in your layout view:
echo $this->layout()->username;
First, read the manual, and than try to accomplish something like this:
class BaseController extends Zend_Controller_Action {
public function preDispatch() {
// your logic to show the user name goes here
}
}
class SomePageController extends BaseController {}
class SomeOtherPageController extends BaseController {}
This will most likely solve your problem.
In my CI system\libraries directory I have a new class named DD_Controller.php. This file looks like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class DD_Controller extends Controller
{
protected $ddauthentication;
function __construct()
{
parent::Controller();
$this->ddauthentication = "Authenticated";
}
}
?>
My application controller is defined like this:
class Inquiry extends DD_Controller
{...}
The Inquiry class works fine when I extend Controller, but I get a
Fatal error: Class 'DD_Controller' not
found in
C:\development\localhost\applications\inquiry\controllers\inquiry.php
on line 4
When I extend DD_Controller. In the config file I have the prefix defined as such:
$config['subclass_prefix'] = 'DD_';
Any idea of what I'm missing?
TIA
This is a better approach. Do the following:
Go to the following directory: your_ci_app/application/core/ and create a php file called MY_Controller.php (this file will be where your top parent classes will reside)
Open this the file you just created and add your multiple classes, like so:
class Admin_Parent extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test() {
var_dump("from Admin_Parent");
}
}
class User_Parent extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test(){
var_dump("from User_Parent");
}
}
Create your children controllers under this directory your_ci_app/application/controllers/ . I will call it adminchild.php
Open adminchild.php and create your controller code, make sure to extend the name of the parent class, like so:
class Adminchild extends Admin_Parent {
function __construct() {
parent::__construct();
}
function test() {
parent::test();
}
}
DD_Controller.php should be in /system/application/libraries/
If you're using the same CI for multiple apps, and you want them all to be able to extends their controllers to your custom one then you can extend the base Controller class in the same file.
In system/libraries/Controller.php below the Controller class:
class Mega_Controller extends Controller {
function Mega_Controller()
{
parent::Controller();
// anything you want to do in every controller, ye shall perform here.
}
}
Then you'll be able to do this in your app controllers:
class Home extends Mega_Controller {
....
Since the extended controller class you created will be available. I think this is better then overwriting the base controller, but that would work as well.
I recommend to avoid "cracking" CodeIgniter core files.
Better use its native extending possibilities and try to fit into them.
The same rule I would recommend for any PHP library / CMS.
This rule has few reasons:
- ability to quiclky upgrade without takint into account thousands of notes where and how was cracked in core files;
- portability;
- possibility to share your code - eg, this will be usable by both you and your friends in case of need, and it will help them to keep their library up to date, the same as you.
In other words, this is much more professional and it pays to you in the future by usability, portability and by update application possibility.
Regarding your personal question...
As for me, there is nothing bad to create your own library with everything you need to extend native CodeIgniter Controller, then load this library in Controller's constructor and you are done. The only thing to make better usability is to give short name to your library.
This way you can even divide what you need in different pieces and put into separate libraries:
WebFeatures
AdminFeatures
etc.
Then you just load needed libraries in your controller's constructor and you are done.
P.S. I know that proposed way does not fit into "right" OOP concept, but in the same time you must never forget about the integrity of the libraries used.
Everything above is just one more view of mine 7-years experience in professional web development, so I hope it will be helpful if not to follow, then at least to take into account.
Regards,
Anton
I had been wondering why my error page caused certain pages of my site
not to render, but then I realized that it's because AppError extends
ErrorHandler instead of AppController. This caused some variables
that I set in AppController's beforeFilter method not to be sent to
the view. Since I can't access session variables from AppError, I
thought that I might be able to get away with using the classRegistry
to instantiate something that could and simply copying and pasting the
rest of my code from AppController's beforeFilter... but that isn't working, nor does it seem like a very elegant fix. Does anyone have any clues as to what
would be the best way to approach this? Thanks, David.
Your AppError class has a controller instance. You can call the beforeFilter manually:
<?php
class AppError extends ErrorHandler {
function error404() {
$this->controller->beforeFilter();
parent::error404();
}
}
?>
In CakePHP 2, you can do something like this to achieve the same effect. In app/Config/bootstrap.php, add this line:
Configure::write('Exception.renderer', 'AppExceptionRenderer');
Then create a file app/Lib/Error/AppExceptionRenderer.php with this code:
App::uses('ExceptionRenderer', 'Error');
class AppExceptionRenderer extends ExceptionRenderer {
protected function _outputMessage($template) {
$this->controller->beforeFilter();
$this->controller->render($template);
$this->controller->afterFilter();
$this->controller->response->send();
}
}
Described more generally here: http://book.cakephp.org/2.0/en/development/exceptions.html#using-a-custom-renderer-with-exception-renderer-to-handle-application-exceptions
Edit: Updated link to point to correct location of the CakePHP 2.0 Book as of July 05, 2012.