Zend modules with form - zend-framework

I am new to Zend Framework. I am using modules for front end and admin panel.
In the admin panel, I would like to have login form, then the folder structure is
application/
modules/
backend/
controllers/
LoginController.php
forms/
LoginForm.php
views/
scripts/
login/
index.phtml
I am having an error "Fatal error: Class 'Backend_Forms_LoginForm' not found in D:\wamp\www\ioc\Application\modules\backend\controllers\LoginController.php on line 9"
Please help me to fix this.
Thanks.
I created controller and forms using following URL http://weierophinney.net/matthew/archives/165-Login-and-Authentication-with-Zend-Framework.html
In the controller, it call the form by
return new Backend_Forms_LoginForm(array(
'action' => '/login/process',
'method' => 'post',
));
Please help me to fix this.

If you're using standard autoloading, your form's class should be named exactly Backend_Form_LoginForm ('form' in singular).
Also make sure that you have a bootstrap for Backend module in place.

I don't know why it won't work... it sould...
by the way, a lot of developers that I met use to put all the forms into the default form directory or into the Library directory, so the can change the name as they prefer without doing includes or something else.
in your case you can try create /library/backend/forms and write the form in the same way as you writed

Related

Zend Framework deployment to a subdomain

I am using Zend Framework to develop some application. I develop on my localhost and then I upload it to some subdomain like abc.mydomain.com. When I upload my layout index.phtml, I get the following for all URLs:
abc.mydomain.com/css/base.css 404 (Not Found)
on the web browser console but my CSS is in the public folder.
my code to link this css is :
<?php echo $this->headLink()->appendStylesheet('/css/base.css') ?>
What should i do to make these addresses work ?
To complete my question : How does the framework distinguish between a folder and a controller or How could i tell the framework to don't act on these URL ass action , they are folders ?
Sounds like a mapping issue associated to the hosting the subdomain. Often on shared hosting you cannot map the project's public folder to the subdomain. So a variety of rewrite tricks are required to map requests for pages and assets into the right directories without confusing ZF about the requested URL.
Check out:
http://www.papayasoft.com/2010/05/08/zend-framework-shared-hosting/
for a description of the problem and various approaches to solving it.
Usually, the baseUrl() view-helper detects (internally, using the Zend_Controller_Front::getBaseUrl() method) what your app's base-url is.
But sometimes, depending upon your vhost setup and the location of your public folder within the doc root - you need to tell the app explicitly what your base-url is.
In application/configs/applicatiom.ini, you can set:
resources.frontController.baseUrl = "http://abc.mydomain.com"
Then in a view-script, you can access public assets using the baseUrl() view-helper and a relative url (relative to the base you set), as follows:
<?php echo $this->headLink()->appendStylesheet($this->baseUrl('css/base.css')) ?>
though I see many people use it as a prefix-only:
<?php echo $this->headLink()->appendStylesheet($this->baseUrl() . '/css/base.css') ?>

url links in Zend Layout automatically appended by ~username on server

I'm using ZF for my project and my server directory structure is:
/ROOT
__/APPLICATION
__/Zend library
__/public_html(I put all the contents of public folder created by ZF here)
__/docs
__library
I have a single .htaccess file which I put in public_html folder. There are two issues that I want help for.
First,
the url links I'm creating using $this->url(array('controller'=>'home', 'action'=>'index'),null,true), for example, are resulting into <a href='/~wethemen/home'>...</a>, where 'wethemen' is my username on the hosting server account. I checked that in page source. That's why it is not rendering the requested controller and actions as well, may be.
Second,
Only the layout is rendered and no action. My default controller is 'home' so I get this error when I try to access the site.
script:''home'/index.phtml' not found in path (/home1/wethemen/application/views/scripts/).
This is the first time I'm deploying a ZF project on server. Any help will be greatly appreciated. I'll pour the contents of index.php and bootstrap.php if needed.
Just put
$controller = Zend_Controller_Front::getInstance();
$controller->setBaseUrl('/your/base/url');
where you find the text "->setRouter" or anywhere in bootstrap, before dispatch() is called.
Edit: if this does not work and you think it is a HTTP server rewrite issue, adding
RewriteEngine on
RewriteBase /
if you application is accessed at http://yourdomain.com/
or
RewriteEngine on
RewriteBase /your/base/url/
if the application is accessed as http://yourdomain.com/your/base/url/

Zend framework redirector doesn't work properly

I'm trying to develop a site using Zend Framework, but i have a problem:
I have the default controller which shows me a page with a link. That link uses another controller and redirects me to a start page from that controller (i have implemented startAction() in my custom controller, and the start.phtml page in the modules/default/view/scripts/disc directory).The start view contains a table which is populated from a database, and three link: Add, Edit and Delete. For the Add link i use in start.phtml the following code:
<p>Add</p>
And for the other two link something like that except the action name. First time when i use one of the links, everything works fine, but when it returns me to the start page all the links don't work anymore. They just keep me in the start page. In my addAction() i do the things i need, and at the end i use the following code to return to start page :
$this->_helper->redirector ( 'start' );
and when the start page is loaded again the 'Add' link points to the start page and not anymore to the Add page. The same thing happens with the other two links.
Can anyone help me, please ?
you are attempting to use the short hand version of the action helper Redirector()
$this->_helper->redirector ( 'start' );
To avoid confusion with the utility method version (and because I don't know what the exact defaults are) I always use the proper form.
//the redirector helper has to many options to comfortably short hand.
//gotoSimple(), gotoUrl() and gotoRoute() are all easy to use.
$this->_helper->getHelper('Redirector')->gotoSimple('action' => 'start', 'controller' => 'index')
to use the utility method _redirect(), try something like this (it acepts a url):
$this->_redirect('/index/start');
I think that start must be its own route. As a result, you need to specify the default route when using the url view helper:
In start.phtml, try this:
<p><a href="<?php echo $this->url(
array ('controller' => 'disc','action' => 'add'), 'default');
?>">Add</a></p>

Zend Framework Multi Module Access?

I have two modules in my application Admin and Default (Shopping Cart).
In My Admin Module I have added 3 Controllers Products, Category and Users
I want to add 3 Links in My Layout but it will cause error
Links to be added as:
Products
When I Click on This Link It will show me error
Message: Route admin is not defined
Give Answer with Example
Thanks
The second parameter to the url view helper is for the router name. If you haven't defined a custom router, then you don't use it. What you need to do, is put 'module' in the array for the first parameter, like this:
<?php echo $this->url(array('module'=>'admin','controller'=>'index', 'action'=>'index')); ?>

Can I integrate a Zend-Framework powered web application into a wordpress site?

I have a project in which I want to be able to call wp_list_pages() on a page that also uses the Zend Framework to power some complex interfaces manages custom data outside of wordpress.
This page should also redirect the user to the wordpress login screen if they're not already logged in with the appropriate level of authorization.
How would this work at a high level, i.e. do I need to edit the wordpress bootstrap file to conditionally implement the custom interface based on a specific URL or something, but still include certain files to be able to call wp_list_pages() on that custom interface?
I've developed a couple of WordPress plugins, and I've found it's really easy to extend. Haven't worked with Zend though.
You should check the WordPress plugin api. Mostly the part about actions, filters and hooks: http://codex.wordpress.org/Plugin_API
You can even override some functions (not sure if wp_list_pages() is overridable).
It's pretty well documented, and there's a large developer community behind it on IRC, forums, etc.
Thanks Fernando.
I just read this thread which suggests that you can use Zend in any script by just including:
require_once('Zend/Loader.php');
Zend_Loader::registerAutoload();
So given that all I need to use Zend for is on one page, can I just include that code in a custom template file that I assign to the appropriate page in the navigation? If I used javascript to submit the form via XHR, the requested URL would take the form '/controller/action' - but Zend wouldn't know the controller directory.
Could I put Zend code into the wordpress bootstrap, i.e. the above code plus the frontController configuration, and then use Zend wherever however?
So I've created a page in Wordpress and a custom template for that page, in which I've placed the following Zend Framework code:
require_once('Zend/Loader.php');
Zend_Loader::registerAutoload();
$db = Zend_Db::factory('Pdo_Mysql', array(
'host' => 'localhost',
'username' => 'username',
'password' => 'password',
'dbname' => 'dbname'
));
Zend_Db_Table::setDefaultAdapter($db);
class Users extends Zend_Db_Table_Abstract {
protected $_name = 'wp_users';
}
$users = new Users();
$users = $users->fetchAll()->toArray();
print_r($users[0]['user_login']);
This all works fine, so it's clearly possible to use Zend in conjuction with Wordpress at least to some extent.
It's becoming apparant that the problem is about who controls the URL rewriting, or the routing, or the bootstrapping (not sure of the correct terminology). If I were to put the end of the above code, starting $users = new Users();, into a controller as follows:
class UsersController extends Zend_Controller_Action {
function getUserAction() {
$this->_helper->viewRenderer->setNoRender();
$users = new Users();
$users = $users->fetchAll()->toArray();
echo $users[0]['user_login'];
}
}
How would I then call that function? My intention would be to call it from javascript via an XHR request in response to an event on the page, but requesting the URL 'index.php/Users/getUser/' returns 'No input file selected'. Trying to access the URL http://www.domain.com/Users/getUser/ produces a Wordpress 404 page.
Is there a way around this? It doesn't just apply to wordpress, of course - I expect it applies to any existing application that rewrites/routes requests via a bootstrap.
I guess you could do that, just import the framework into the one page you need it for. I don't know how Zend works, but check the paths as to where to put your directories so that Zend finds them.As I said I guess you could do that, just experiment and tell us how it went!
Beware of name conflicts for functions and/or variables, this shouldn't be much of a problem coming from such popular products as WordPress and Zend though... (which should be theoretically well coded)
I guess you could do that, just import the framework into the one page you need it for. I don't know how Zend works, but check the paths as to where to put your directories so that Zend finds them.As I said I guess you could do that, just experiment and tell us how it went!
Beware of name conflicts for functions and/or variables, this shouldn't be much of a problem coming from such popular products as WordPress and Zend though... (which should be theoretically well coded)
I've built a plugin for wordpress that has a similar goal to yours, more modeled on CodeIgniter though. Not knowing Zend terribly well, I think this should help:
Make a file named routes.php in your plugins directory with the following code:
add_action( 'init', 'add_custom_urls' );
function add_custom_urls(){
global $wp, $wp_rewrite;
$wp_rewrite->add_rule( '(.*)$', 'index.php?&cPath=$matches[1]', 'top' );
$wp->add_query_var( 'cPath' );
}
Be sure to activate both plugins in your admin. These two files will allow you to catch the url before Wordpress tries to figure out what to do with it. You can use regular expressions to have finer control over which pages to catch. You may have to delete the record in your _options db table where option_name = 'rewrite_rules' before this works.
Next, make another plugin with the following code:
add_action( 'template_redirect', 'bootstrap' );
function bootstrap(){
global $cPath;
echo( "cPath : $cPath" );
if( $cPath ){
dosomethingwith( $cPath );
}
}
Put all your code in the dosomethingwith() function. You'll need to figure out if the url requested can me mapped to a zend controller, etc. http://www.domain.com/Users/getUser/ would give you $cPath = Users/getUser/ If successful, you'll also probably want to die(), so once it is completed Wordpress won't try and take over again.