im following some example for doing login in Zend
using Zend_Form
but i get an error when trying to load a view
Fatal error: Class 'Application_Form_Login' not found in
/Users/manuel/projects/zend_template/zend_template/application/controllers/AuthController.php
on line 13
this is the code on that file on that line
public function indexAction()
{
[line 13] $form = new Application_Form_Login();
...}
i Have a form on
application\forms\Login.php
created on CLI
and this one have the form code
class Application_Form_Login extends Zend_Form
{
public function init()
{ ...
So, i have my Application_Form_Login
but what is the problem?
thanks
try checking your application.ini file to see if it has the 'Application' namespace registered. If your models/forms etc begin with Application_ you have to configure the autoloader to find them.
In application.ini add:
appnamespace = "Application"
Or in your bootstrap class add this:
protected $_appNamespace = 'Application';
Related
I know that I can make a file called MY_Form_validation based on the default
$config['subclass_prefix'] = 'MY_';
But what if I have different forms that I want to represented with different MY_Form_validation classes? If this is possible, how will I load and call it in my validation controller?
For example MY_Student_registration_form_validation, MY_Student_cancellation_form_validation, and so on?
Most of the question online just straightforward suggest creating this sole class but not for multiple instances.
NOTE I tried creating MY_Student_registration_form_validation class inside application/libraries (CI 3.x) and loaded it in my controller using
$this->load->library('my_student_registration_form_validation');
and I got the error Non-existent class: My_student_registration_form_validation.
Then I tried without the prefix MY
$this->load->library('student_registration_form_validation');
and I got a different one Unable to load the requested class: Student_registration_form_validation.
Alright kids I got it now.
Create your custom form validation class with the prefix MY_ or whatever you declared in the application/config/config.php file. For example, MY_Student_registration_form_validation inside application/libraries for CI 3.x.
$config['subclass_prefix'] = 'MY_';
In your controller (I will use controller here since I haven't tested it on a model), you can load this custom class by:
$this->load->library('my_student_registration_form_validation');
You can now use its methods by:
$this->my_student_registration_form_validation->validate_student();
Put this piece at the end of APPPATH.'config/config.php' file
spl_autoload_register(function ($class) {
//this block you can skip
if (substr($class,0,3) !== 'CI_') {
if (file_exists($file = APPPATH.'core/'.$class.'.php')) {
include $file;
}
}
//this block is what you need
if (substr($class,0,3) !== 'CI_') {
if (file_exists($file = APPPATH.'libraries/'.$class.'.php')) {
include $file;
}
}
});
This way, you don't even set config file's predefined MY_ prefix.
Name your files and classes like:
Student_registration_form_validation.php | Student_registration_form_validation
Student_cancellation_form_validation.php | Student_cancellation_form_validation
Some_controller_code.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Some_controller_code extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
//you have to load form_validation library first
//if custom libraries are extending it
$this->load->library('form_validation');
$this->load->library('student_registration_form_validation');
//example of using method from native form_validation library
if ( $this->student_registration_form_validation->set_message('rule1', "Some message here") )
echo 'It\'s working';
}
}
I'm developing a personal finance application with ZF2 in ZendStudio 11. I'm getting the following fatal error after adding a new controller from New Zend Item wizard. Here's a screenshot.
Error:
_Fatal error: Cannot redeclare class Application\Controller\OutcomeController in **C:\wamp\www\PersonalFinance\module\Users\src\Users\Controller\OutcomeController.php** on line 33_
OutcomeController class code is as follow:
<?php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
/**
* OutcomeController
*
* #author
*
* #version
*
*/
class OutcomeController extends AbstractActionController
{
/**
* The default action - show the home page
*/
public function indexAction()
{
// TODO Auto-generated OutcomeController::indexAction() default action
return new ViewModel();
}
public function fooAction()
{
// This shows the :controller and :action parameters in default route
// are working when you browse to /income/income/foo
return array();
}
}
I've also added the below line to the autoload_classmap.php file:
'Users\Controller\OutcomeController' => __DIR__ . '/src/Users/Controller/OutcomeController.php',
So:
I've added the controller using the wizard
I've added the needed
controller configs to the module.config.php
I've added the
autoloading config line to the autoload_classmap.php
Any ideas why the error happens?
As the error message suggests, the class Application\Controller\OutcomeController is already defined somewhere else in your application.
This has nothing to do with using the wizard of ZendStudio, configuring correctly ZF2 or with autoloading. This is a PHP thing: you can't have in your application two classes with the same name (considering the namespace).
To avoid the problem you could either change the name of the class or change the namespace where you declare it.
I am trying to call a third party library function in CakePHP from a controller file.
I have this in my controller file:
public function index() {
App::import('vendor', 'simple-html-dom.php');
App::import('vendor', 'utils.php');
set_time_limit(0);
$html = file_get_html("google.com");
...
}
I also have in app/vendor both simple-html-dom.php and utils.php files.
file_get_html is a public function in simple-html-dom.php (and it doesn't belong to any class). I end up with this error:
Error: Call to undefined function file_get_html()
I have been trying to search for how to solve this, but I have not found an answer.
I got mine working. Try this,
App::import('Vendor', 'simple_html_dom', array('file'=>'simple_html_dom.php'));
$html = file_get_html("google.com");
Try
public function index() {
App::import('vendor', 'simple-html-dom.php');
App::import('vendor', 'utils.php');
set_time_limit(0);
$SimpleHtmlDom = new SimpleHtmlDom(); // create object for html dom
$html = $SimpleHtmlDom->file_get_html("google.com");
}
Make sure simple-html-dom.php file contain class then you need to create object of that class after loading vendor.
because to access methods and property of class you need to create object of that class.
You can also access method with in same class using Self::file_get_html(); but this is for inside class declaration.
More help
App::import('Vendor', 'example', array('file' => 'Example.php'));
$example = new Example();
in above code i am including vendor file.
explanation
Above code will load Example.php file which is inside vendors/example directory.
In your case your vendor file is not properly loaded that's why you are getting class not found error.
class SomeController extends Controller
{
public function actionIndex() {
echo 'This is some controller';
}
}
class AnotherController extends SomeController
{
public function actionIndex() {
echo 'This is another controller';
}
}
This works:
index.php?r=some
but ...
index.php?r=another
says:
PHP warning
include(SomeController.php): failed to open stream: No such file or directory
Both of the files are in
test\protected\controllers\
BTW in the past I also tried using the Gii Controller Generator with "SomeController" as the base class...
It said:
The controller has been generated successfully. You may try it now.
Generating code using template
"C:\xampp\htdocs\yii\framework\gii\generators\controller\templates\default"...
generated controllers\YetAnotherController.php
generated views\yetAnother\index.php
done!
When I clicked on "try it now" it also said:
PHP warning
include(SomeController.php): failed to open stream: No such file or directory
Edit:
Classes inside protected/controllers are not autoloaded, therefore you'll have to import the parent class file before extending from it:
In AnotherController.php:
Yii::import('application.controllers.SomeController');
public class AnotherController extends SomeController {
// ...
}
Incase you need to access the base class from url also, you can use the above method. Otherwise you can put your base class inside protected/components as you have already figured out.
Yii autoloading works only when you have the same name for the file as the class that the file contains. Meaning class SomeController should be within SomeController.php file.
Make those changes and it should work.
A helpful wiki: Understanding Autoloading Helper Classes and Helper functions.
Guide link:
Class files should be named after the public class they contain.
To extend any class just go to the config file and add the class in the import section
'import' => array('application.controllers.SomeController')
this will make it available in the entire application without importing explicitly.
I've the next structure
-application
--controllers
---Sub
----DemoController.php
---IndexController.php
-models
-views
-Boostrap.php
Well, in my Sub/DemoController.php I've the next:
<?php
class Sub_DemoController extends Zend_Controller_Action
{
public function indexAction()
{
echo 'hello demo';
}
}
And my IndexController.php like this:
<?php
class IndexController extends Sub_DemoController
{
}
When I run my web throw the next error: Class 'Sub_DemoController' not found ...
I try with initialize class with Application_Sub_DemoController, but return the same result.
I don't want use modules, I know how uses modules in Zend Framewoork but I don't looking for it.
Any ideas?
You need to add resourceType to your resource autoloader add this to you bootstrap file:
$this->getResourceLoader()
->addResourceType('sub', 'controllers/Sub', 'Sub');
More: http://framework.zend.com/manual/1.12/en/zend.loader.autoloader-resource.html
Please have a look at Zend framework (1.7.5): how to change controller directory
I think you didn't set the controller directory properly or your autoloader does not work.