Zend framework : Cannot access database configuration through application.ini - zend-framework

I am new to Zend. The problem i am getting is that i am not able to access resources.db.* configurations from application.ini
The way i am using to access is
$application->getOptions()
. It does not shows the resource.db.* properties.
Can any one help me ?

Do this instead
$params = Zend_Db_Table::getDefaultAdapter()->getConfig(); //return associative array

To do it the way you asked about, the easiest way I've found is to put everything in the registry during bootstrap:
//bootstrap.php
public function _initConfig {
$config = new Zend_Config($this->getOptions());
Zend_Registry::set('config', $config);
}
to use these configs elsewhere in your application:
$db = Zend_Registry::get('config')->resources->db;
although if you are just trying to access the adapter registered in the application.ini:
$db = Zend_Db_Table::getDefaultAdapter();

Related

Dealing with Zend Auth Data when using zend modules

I am working on zend module based structure for my project. As Zend_Auth class' default session storage is Zend_Auth. I changed according to module being called. Say for admin I use auth namespace Admin_Auth and for default module i use namespace name Default_Auth.
$auth = Zend_Auth::getInstance();
$auth->setStorage(new Zend_Auth_Storage_Session('Admin_Auth'));
I am doing this because, if I do Zend_Session::destroy() it will destroy complete session even for Default Module. and so using namespace and so at logout Zend_Session::namespaceUnset('Admin_Auth');
each time in different controller I have to use
$auth = Zend_Auth::getInstance();
$auth->setStorage(new Zend_Auth_Storage_Session('Admin_Auth'));
just to point corresponding session data.
I am considering to move it in module's Bootstrap.php like
protected function _initAuth(){
$auth = Zend_Auth::getInstance();
$auth->setStorage(new Zend_Auth_Storage_Session('Admin_Auth'));
return $auth;
}
First, is that proper way?
Second, If it is then how can I access the return value $auth of _initAuth() in each controller? Please

Zend Routes translate URL's

1) I have a controller "calendar" and have action "showDate" which gets date via url. So, url is something like "calendar/show-date/date/2012-07-22"
2) I have a link to display all entries, "calendar/"
So, I want to create routes so my links look like "kalendar/2012-07-22" and "kalendar/".
Can anyone help me?
According to this post:
http://www.z-f.fr/forum/viewtopic.php?id=5138
The solution is to add '#locale' => $lang to the params.
$this->url(array('lang'=>'it','#locale'=>'it'))
It works very well for me.
I've been looking into translating the URL with Zend_Translate and I came across this sites' plugin that attempts to auto-translate URL segments (module/controller/action).
http://blog.helmich.cz/305-howto-simple-multilingual-routes-in-zend-framework/
The nice thing is that it's a modified custom router class that can function similar to Zend_Router so it's relatively familiar off the bat.
$pages = new MyApp_Controller_Router_Route(
':locale/:#controller/:#action/*',
array(
'controller' =>; 'index',
'action' => 'index',
'locale' => 'cs'
)
);
$router->addRoute('pages',$pages);
The thing you'll need is to have a language ID in your URL (called :locale in the above example) so your Zend_Translate can set the proper language.
www.example.com/en/calendar/2012-06-22/
www.example.com/fr/calendrier/2012-06-22/
www.example.com/de/kalender/2012-06-22/
www.example.com/it/calendario/2012-06-22/
I've only slightly played around with this concept but I recall that it had promise. You'll have to get more familiar with Zend_Translate: http://framework.zend.com/manual/en/zend.translate.html
I hope that helps!
Cheers!
You could re-route all calls of calendar to kalendar. There are two possibilites, either you do it with Zend (preferable) or you change your webserver configuration to rewrite calls to calendar with a HTTP 302 (ugly).
You should however consult the official Zend Documentation, which is pretty good
You have to setup custom routes, this is my way:
in folder application/configs/ create file named "routes.ini"
Put in file your route:
;index-homepage, parameter date isn't required
;"index" is key of your route
routes.index.route = "kalendar/:date"
routes.index.defaults.controller = calendar
routes.index.defaults.action = show
routes.index.defaults.date =
So in your bootstrap.php define that config file:
protected function _initRoute() {
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addDefaultRoutes();
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini');
$router->addConfig($config, 'routes');
}
And that's it, you can call URL
www.website.com/kalendar
and
www.website.com/kalendar/2012-1-1
See answers in this question for details:
Simple rewrites in Zend Framework

Zend auth inside doctrine2 entity repository

I am trying to create a method inside a doctrine 2 entity in zend framework. It is just to keep the code DRY. I want to retrieve the user object if they are logged in, and FALSE other wise:
public function getCurrentUserId() {
//returns false if not logged in, user object otherwise
$auth = Zend_Auth::getInstance();
$id = $auth->getidentity();
$user = $this->_em->getRepository('Entities\User')
->findOneByid($id);
if (is_null($user))
return false;
else
return $user;
}
}
This works fine within a controller action, but causes the following error here:
PHP Fatal error: Doctrine\Common\ClassLoader::loadClass(): Failed opening required '/var/www/myswap/application/models/Repositories/Zend_Auth.php'
Why, and how can I avoid this?
I'm going to take a guess and assume you're using namespaces since it looks like that.
On the line where you use Zend_Auth, prefix it with a \ - eg. $auth = \Zend_Auth::getInstance();
The reason for this is that namespaces in PHP are relative. Thus, if you try to use just Zend_Auth it assumes you want an object in the current namespace. By prefixing it with a \, you're telling it you want Zend_Auth from root.
I'd suggest familiarizing yourself with the namespaces manual page

Zend Validator location

Do I need to save my custom Zend Validator location to "Zend/Validate"? I'd rather create a folder for all my custom validation scripts, but cannot find anything in the Zend documentation other than changing the namespace.
Here is my error message.
"Plugin by name 'UserNameValidate' was not found in the registry; used paths: Zend_Validate_: Zend/Validate/"
I'd like to tell it to search additional paths.
I was able to solve my problem using
addElementPrefixPath('Application_Validate',
'../application/validate',
'validate');
Did you check the API docs? Zend_Validate has an addDefaultNamespaces method...
public static function addDefaultNamespaces($namespace){
I usually keep my custom validators (e.g. My_Validate_Age) in APPLICATION_PATH/validators named, e.g. . In this case the php file would be: APPLICATION_PATH/validators/Age.php. With this setup I need to add the validators path to the Zend_Autoloader. For this purpose in the Bootstrap.php I have:
protected function _initAutoload() {
$autoLoader = Zend_Loader_Autoloader::getInstance();
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
));
$resourceLoader->addResourceType('validate', 'validators/', 'My_Validate_');
$autoLoader->pushAutoloader($resourceLoader);
}
Hope this will be of help to you.

How to change ldap password using zend

I am working with zend framework, PHP , Ldap on Ubuntu. I am authenticating users from ldap using zend library. Now I want to change user's ldap passwords using zend. Any Idea?
This is the method that I am using to get zend authentication adapter. It is working perfectly and users are authenticated using this adapter.
public function getAuthAdapter(array $params)
{
$front = Zend_Controller_Front::getInstance();
$options = $front->getParam('bootstrap')->getOption('ldap');
$params['username'] = split( "#" , $params['username'] );
$username = 'cn=' . $params['username'][0] . ',' . $options['server1']['baseDn'];
$adapter = new Zend_Auth_Adapter_Ldap( $options, $username, $params['password']);
$adapter->setIdentity( $params['username'] );
$adapter->setCredential( $params['password'] );
return $adapter;
}
Now how to change ldap passwords? Thanks
$ldap=new Zend_Ldap($options);
$ldap->bind();
$entry=$ldap->getEntry($user->dn);
$entry['userpassword'][0]=$newpassword;
$ldap->save($user->dn, $entry);
This is how it worked form me! Note that I did not encript the password before sending but the server stored it with correct encription.
Use Zend_Auth_Adapter_Ldap for authenticating logins and such with an active directory.
For admin purposes of ldap, use Zend_Ldap.
Read the Zend documentation on the Zend_Ldap API, specifically the following
Zend_Ldap save(string|Zend_Ldap_Dn $dn, array $entry)
Saves the entry identified by $dn with
its attributes $entry to the LDAP
tree. Throws a Zend_Ldap_Exception if
the entry could not be saved. This
method decides by querying the LDAP
tree if the entry will be added or
updated.