I am having trouble with zend framework and connection with database, I'm trying to create a login, the table is created, and the code apparently is fine, but gives me this:
An error occurred
Application error
Exception information:
Message: SQLSTATE[HY000] [1045] Access denied for user 'onetag51_teste'#'localhost' (using password: YES)
Stack trace:
#0 C:\xampp\Zend\library\Zend\Db\Adapter\Pdo\Mysql.php(109): Zend_Db_Adapter_Pdo_Abstract->_connect()
#1 C:\xampp\Zend\library\Zend\Db\Adapter\Abstract.php(860): Zend_Db_Adapter_Pdo_Mysql->_connect()
#2 C:\xampp\Zend\library\Zend\Db\Adapter\Abstract.php(930): Zend_Db_Adapter_Abstract->quote('pass1', NULL)
#3 C:\xampp\Zend\library\Zend\Auth\Adapter\DbTable.php(449): Zend_Db_Adapter_Abstract->quoteInto('`password` = ?', 'pass1')
#4 C:\xampp\Zend\library\Zend\Auth\Adapter\DbTable.php(368): Zend_Auth_Adapter_DbTable->_authenticateCreateSelect()
#5 C:\xampp\Zend\library\Zend\Auth.php(117): Zend_Auth_Adapter_DbTable->authenticate()
#6 C:\xampp\htdocs\Eulen\application\controllers\AuthenticationController.php(27): Zend_Auth->authenticate(Object(Zend_Auth_Adapter_DbTable))
#7 C:\xampp\Zend\library\Zend\Controller\Action.php(516): AuthenticationController->logininAction()
#8 C:\xampp\Zend\library\Zend\Controller\Dispatcher\Standard.php(308): Zend_Controller_Action->dispatch('logininAction')
#9 C:\xampp\Zend\library\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#10 C:\xampp\Zend\library\Zend\Application\Bootstrap\Bootstrap.php(97): Zend_Controller_Front->dispatch()
#11 C:\xampp\Zend\library\Zend\Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#12 C:\xampp\htdocs\Eulen\public\index.php(26): Zend_Application->run()
#13 {main}
Request Parameters:
array (
'controller' => 'authentication',
'action' => 'loginin',
'module' => 'default',
)
I was searching for a solution, and some of the possible errors were, spelling errors(I didn't find any), can't connect to phpmyadmin using my pass and user(if I go to the phpmyadmim site, I can enter using the username and password).
I have try several solution but I cant figure it out.
Am I missing something.
file config.ini:
resources.db.params.charset = "utf8"
resources.db.adapter = "pdo_mysql"
resources.db.params.host = localhost
resources.db.params.username = "onetag51_teste"
resources.db.params.password = "*******"
resources.db.params.dbname = "onetag51_eulenhugo"
Its better to put here the authenticationcontroller, maybe the problem is there..
<?php
class AuthenticationController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// action body
}
public function logininAction()
{
// action body
$authAdapter =$this->getAuthAdapter();
$username='hugo';
$password='pass1';
$authAdapter->setIdentity($username)
->setCredential($password);
$auth =Zend_Auth::getInstance();
$result = $auth->authenticate($authAdapter);
if($result->isValid())
{
echo 'valid';
}
else
{
echo 'invalid';
}
}
public function logoutAction()
{
// action body
}
private function getAuthAdapter()
{
$authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
$authAdapter->setTableName('users')
->setIdentityColumn('username')
->setCredentialColumn('password');
return $authAdapter;
}
}
ive been talking to a friend of mine, and he told me that i need to set the permission for acesse the phpmyadmim through zend...
How can i do that?
You don't need to use quotations around your adpater, username, password and dbname in your config file. I don't in mine, hope this helps.
I found a bit more reading that might be helpful access denied for user # 'localhost' to database '' because it mentions
You need grant privileges to the user if you want external acess to database(ie. web pages).
in the answer.
There is also this link about adding the users to the database in mysql.
Hope this helps
So the problem was related with the priveleges, thanks for ARJMP, ive tryed changed the priveleges but thee host didnd let me do it, so what i did was create a local database using xamp and try the login with the database that ive created in xamp, and it worked...
Related
I am sending emails from my ZF2 application. I want to create it as a service so that I can use it from any module and any controller.
I have the code to send the emails, need guidance to create service.
protected function sendEMail($msgSubj,$msgText, $fromEmail, $toEmail) {
$mail = new Mail\Message();
$mail->setFrom($fromEmail, $fromEmail);
$mail->addTo($toEmail, $toEmail);
$mail->setSubject($msgSubj);
$mail->setBody($msgText);
$transport = new Mail\Transport\Sendmail();
$transport->send($mail);
return true;
}
I am currently having this function defined in one of the controllers to send the email. However, my application is evolving and I need to send application from multiple modules and multiple controllers within modules.
I understand that this will be easier if I define sending email as a service and call it as required. I have read the documentation and it is considerably heavy. I am looking for examples where someone has done something similar.
Thanks in advance for your time.
2nd Edit on 2014-05-11
I have done the following
1> created a new service class SendEmail with the following code
namespace Application\Service;
class SendEmail {
public function __construct()
{
}
protected function sendEMail($msgSubj,$msgText, $fromEmail, $toEmail) {
$mail = new \Zend\Mail\Message();
$mail->setFrom($fromEmail, $fromEmail);
$mail->addTo($toEmail, $toEmail);
$mail->setSubject($msgSubj);
$mail->setBody($msgText);
$transport = new \Zend\Mail\Transport\Sendmail();
$transport->send($mail);
return true;
}
}
2> defined an entry in module.config.php for the Application Module
under "service_manager" -> "factories"
'SendEmail' => 'Application\Service\SendEmail',
3> defined the function in IndexController
public function getSendEmail( ) {
if ( !$this->sendEmail ) {
$this->sendEmail = $this->getServiceLocator( )->get( 'SendEmail' );
}
return $this->sendEmail;
}
4> at the place where I want to send the email I have the following code
$sendEmail = $this->getSendEmail( );
$sendEmail->sendEMail("subject of email","email body message", "from#email.com", "to#email.com");
5> I added Application\Service\SendEmail to the list of invokables
I am getting the following error
Zend\ServiceManager\Exception\ServiceNotCreatedException
File:
/var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:1036
Message:
While attempting to create sendemail(alias: SendEmail) an invalid factory was registered for this instance type.
Stack trace:
#0 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(612): Zend\ServiceManager\ServiceManager->createFromFactory('sendemail', 'SendEmail')
#1 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(572): Zend\ServiceManager\ServiceManager->doCreate('SendEmail', 'sendemail')
#2 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(504): Zend\ServiceManager\ServiceManager->create(Array)
#3 /var/www/zf2/dwam/module/UserAdmin/src/UserAdmin/Controller/IndexController.php(123): Zend\ServiceManager\ServiceManager->get('SendEmail')
#4 /var/www/zf2/dwam/module/UserAdmin/src/UserAdmin/Controller/IndexController.php(77): UserAdmin\Controller\IndexController->getSendEmail()
#5 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractActionController.php(83): UserAdmin\Controller\IndexController->userLoginAction()
#6 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#7 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#8 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#9 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#10 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/Mvc/DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#11 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#12 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#13 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#14 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(316): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#15 /var/www/zf2/dwam/public/index.php(17): Zend\Mvc\Application->run()
#16 {main}
Basically the service has not been created; what am I missing; What more needs to be done.
Edit on 2014-05-12 Found the solution; not sure if this is the best way though!!!
I would request the group of AlexP, nikoshr, EdChum, Eight-Bit Guru, Tad Donaghe to remove the hold so that I can publish the solution.
I have found the solution and I am posting the code that work well for me; Do comment on any other improvements that can be made.
1> created a new service class SendEmail with the following code
namespace Application\Service;
class SendEmail {
public function sendEMail($msgSubj,$msgText, $fromEmail, $toEmail) {
$mail = new \Zend\Mail\Message();
$mail->setFrom($fromEmail, $fromEmail);
$mail->addTo($toEmail, $toEmail);
$mail->setSubject($msgSubj);
$mail->setBody($msgText);
$transport = new \Zend\Mail\Transport\Sendmail();
$transport->send($mail);
return true;
}
}
2> defined in module.php for the Application Module create the function getServiceConfig()
'factories' => array (
'SendEmail' => function ( $sm ) {
return new \Application\Service\SendEmail( );
},
),
3> defined the function in IndexController
public function getSendEmail( ) {
if ( !$this->sendEmail ) {
$this->sendEmail = $this->getServiceLocator( )->get( 'SendEmail' );
}
return $this->sendEmail;
}
4> at the place where I want to send the email I have the following code
$sendEmail = $this->getSendEmail( );
$sendEmail->sendEMail("subject of email","email body message", "from#email.com", "to#email.com");
I've been following the instruction on how to create custom routes from the book Zend Framework - A Beginners Guide
I've changed my application.ini file to include this routing information:
resources.router.routes.static-content.route = /content/:page
resources.router.routes.static-content.defaults.module = default
resources.router.routes.static-content.defaults.controller = static-content
resources.router.routes.static-content.defaults.view = static-content
resources.router.routes.static-content.defaults.action = display
Given the above configuration, I have this controller:
<?php
class Default_StaticContentController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function displayAction()
{
// action body
$page = $this->getRequest()->getParam('page');
if (file_exists($this->view->getScriptPath(null) .
'/' . $this->getRequest()->getControllerName() . '/' .
$page . $this->viewSuffix
)) {
$this->render($page);
}
else {
throw new Zend_Controller_Action_Exception('HLC - Page not found', 404);
}
}
}
I have a view named about.phtml in the APPLICATION_PATH/modules/default/views/static-content folder.
What ahppens is I get an error saying:
An error occurred
Page not found
Exception information:
Message: Invalid controller class ("StaticContentController")
Stack trace:
#0 /Applications/MAMP/htdocs/zend/library/Zend/Controller/Dispatcher/Standard.php(262): Zend_Controller_Dispatcher_Standard->loadClass('StaticContentCo...')
#1 /Applications/MAMP/htdocs/zend/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#2 /Applications/MAMP/htdocs/zend/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#3 /Applications/MAMP/htdocs/zend/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#4 /Applications/MAMP/htdocs/HLC/public/index.php(26): Zend_Application->run()
#5 {main}
Request Parameters:
array (
'page' => 'about',
'module' => 'default',
'controller' => 'static-content',
'view' => 'static-content',
'action' => 'display',
)
Note that it is not rendering my customised Zend_Controller_Action_Exception but throwing the global error.
I'm using the URL: http://hlc.local:8888/content/about
The default index action works ok, just this routing that's not working.
if you are actually following the book closely, you have an extra line in your route declaration and your controller class should be StaticContentController.
here is the route definition from the book that does work.
resources.router.routes.static-content.route = /content/:page
resources.router.routes.static-content.defaults.module = default
resources.router.routes.static-content.defaults.controller = static-content
resources.router.routes.static-content.defaults.action = display
I still have this code laying around from last summer.
I found this book less then satisfactory and not really for beginners. It fails to address the Zend_Db component opting instead to introduce Doctrine 1.2. It's seems to be a trend that a number of these beginner/easy books feel that a full ORM is more useful then Zend_Db. If you are already familiar with Doctrine this approach works fine, otherwise it's a lot to ask of a beginner, to learn ZF and Doctrine at the same time.
Hope this helps.
I don't now what you use for autoloading. so that would helpful to determine.so far I understand your class named should be something like this ModulePath_ApplicationPath_ControllerName, so its Default_Application_StaticContentController.
and for better routing I preferred zend manual. you can try this tutorial for route. it would help for you.
When I call the "users" controller, it is giving me this error:
**Fatal error:** Uncaught exception 'Zend_Acl_Exception' with message 'Resource 'error' not found' in /home/zerego/library/Zend/Acl.php:364
Stack trace:
#0 /home/zerego/library/Zend/Acl.php(774): Zend_Acl->get('error')
#1 /home/zerego/application/plugins/AccessCheck.php(25): Zend_Acl->isAllowed('normal', 'error', 'error')
#2 /home/zerego/library/Zend/Controller/Plugin/Broker.php(309): My_Plugin_AccessCheck->preDispatch(Object(Zend_Controller_Request_Http))
#3 /home/zerego/library/Zend/Controller/Front.php(941): Zend_Controller_Plugin_Broker->preDispatch(Object(Zend_Controller_Request_Http))
#4 /home/zerego/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#5 /home/zerego/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#6 /home/zerego/public_html/index.php(26): Zend_Application->run()
#7 {main} thrown in /home/zerego/library/Zend/Acl.php on line 364
My acl document works fine with the other controllers, and is like this:
<?php
$acl = new Zend_Acl();
$roles = array('admin', 'normal');
// Controller script names. You have to add all of them if credential check
// is global to your application.
$controllers = array('error', 'auth', 'index', 'paginator', 'albums', 'users');
foreach ($roles as $role) {
$acl->addRole(new Zend_Acl_Role($role));
}
foreach ($controllers as $controller) {
$acl->add(new Zend_Acl_Resource($controller));
}
// Here comes credential definiton for admin user.
$acl->allow('admin'); // Has access to everything.
// Here comes credential definition for normal user.
$acl->allow('normal'); // Has access to everything...
$acl->deny('normal', 'albums'); // ... except the admin controller.
// Finally I store whole ACL definition to registry for use
// in AuthPlugin plugin.
$registry = Zend_Registry::getInstance();
$registry->set('acl', $acl);
?>
I found the answer !
I was working in the wrong Acl file. In the right Acl file i didn't add the "error" ou "users" resources.
Thanks for the help anyway. :)
I'm having trouble with sending email using the zend framework. I keep getting a "Could not open socket" error.
I don't know whats wrong here - it used to work on my other host. Ever since I shifted it to another host I can't send emails. I've set up the configuration values to match the new email server.
Heres my code:
$config = array('auth' => _config('mail', 'auth'),
'username' => _config('mail', 'email'),
'password' => _config('mail', 'password'));
$tr = new Zend_Mail_Transport_Smtp(_config('mail', 'smtp'), $config);
$mail = new Zend_Mail();
$mail->setDefaultTransport($tr);
$mail->setFrom(_config('mail','email'), _config('mail','name'));
$mail->addTo($account_email);
$mail->setSubject($mailTitle);
$mail->setBodyText($mailContent);
$mail->send($tr);
EDIt ===
Well the code posted above is my actual code - I don't know whats wrong with it as it used to work on another host.
The following is the exact error I'm getting
Could not open socketstring(1237) "#0 /home/india/public_html/demo/library/Zend/Mail/Protocol/Smtp.php(167): Zend_Mail_Protocol_Abstract->_connect('tcp://mail.indi...')
#1 /home/india/public_html/demo/library/Zend/Mail/Transport/Smtp.php(199): Zend_Mail_Protocol_Smtp->connect()
#2 /home/india/public_html/demo/library/Zend/Mail/Transport/Abstract.php(348): Zend_Mail_Transport_Smtp->_sendMail()
#3 /home/india/public_html/demo/library/Zend/Mail.php(1194): Zend_Mail_Transport_Abstract->send(Object(Zend_Mail))
#4 /home/india/public_html/demo/application/controllers/AccountController.php(2153): Zend_Mail->send(Object(Zend_Mail_Transport_Smtp))
#5 /home/india/public_html/demo/library/Zend/Controller/Action.php(513): AccountController->forgetPasswordAction()
#6 /home/india/public_html/demo/library/Zend/Controller/Dispatcher/Standard.php(295): Zend_Controller_Action->dispatch('forgetPasswordA...')
#7 /home/india/public_html/demo/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#8 /home/india/public_html/demo/application/bootstrap.php(26): Zend_Controller_Front->dispatch() #9 /home/india/public_html/demo/html/index.php(4): Bootstrap::run() #10 {main}"
marhaba Ali! ,
digging the code of Zend mail shows http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Mail/Protocol/Abstract.php
protected function _connect($remote)
{
$errorNum = 0;
$errorStr = '';
// open connection
$this->_socket = #stream_socket_client($remote, $errorNum, $errorStr, self::TIMEOUT_CONNECTION);
if ($this->_socket === false) {
if ($errorNum == 0) {
$errorStr = 'Could not open socket';
}
/**
* #see Zend_Mail_Protocol_Exception
*/
require_once 'Zend/Mail/Protocol/Exception.php';
throw new Zend_Mail_Protocol_Exception($errorStr);
}
if (($result = $this->_setStreamTimeout(self::TIMEOUT_CONNECTION)) === false) {
/**
* #see Zend_Mail_Protocol_Exception
*/
require_once 'Zend/Mail/Protocol/Exception.php';
throw new Zend_Mail_Protocol_Exception('Could not set stream timeout');
}
return $result;
}
and usually the error number 0 because of
if ($errorNum == 0) {
$errorStr = 'Could not open socket';
}
from : http://php.net/manual/en/function.stream-socket-client.php
On failure the errno and errstr
arguments will be populated with the
actual system level error that
occurred in the system-level connect()
call. If the value returned in errno
is 0 and the function returned FALSE,
it is an indication that the error
occurred before the connect() call.
This is most likely due to a problem
initializing the socket. Note that the
errno and errstr arguments will always
be passed by reference.
I guess its some firewall blocking the connection to be sent out ,
system-level or network-level error
If you update your answer with more detailed info , i would be happy to help
So followed all the basic tutorials. Am using Zend 1.8 but i keep getting this exception when using Zend_Auth_DB. Any ideas, there is only one row in the table.
Exception information:
Message: The supplied parameters to Zend_Auth_Adapter_DbTable failed to produce a valid sql statement, please check table and column names for validity.
Stack trace:
#0 C:\projects\BHAA_ZEND\library\Zend\Auth\Adapter\DbTable.php(306): Zend_Auth_Adapter_DbTable->_authenticateQuerySelect(Object(Zend_Db_Select))
#1 C:\projects\BHAA_ZEND\library\Zend\Auth.php(117): Zend_Auth_Adapter_DbTable->authenticate()
#2 C:\projects\BHAA_ZEND\application\controllers\LoginController.php(79): Zend_Auth->authenticate(Object(Zend_Auth_Adapter_DbTable))
#3 C:\projects\BHAA_ZEND\application\controllers\LoginController.php(38): LoginController->getAuthAdapter(Array)
#4 C:\projects\BHAA_ZEND\library\Zend\Controller\Action.php(512): LoginController->processAction()
#5 C:\projects\BHAA_ZEND\library\Zend\Controller\Dispatcher\Standard.php(288): Zend_Controller_Action->dispatch('processAction')
#6 C:\projects\BHAA_ZEND\library\Zend\Controller\Front.php(945): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#7 C:\projects\BHAA_ZEND\library\Zend\Application\Bootstrap\Bootstrap.php(77): Zend_Controller_Front->dispatch()
#8 C:\projects\BHAA_ZEND\library\Zend\Application.php(328): Zend_Application_Bootstrap_Bootstrap->run()
#9 C:\projects\BHAA_ZEND\public\index.php(33): Zend_Application->run()
#10 {main}
Here's the offending code.
public function getAuthAdapter(array $params)
{
$dbAdapter = Zend_Db_Table::getDefaultAdapter();
$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
$authAdapter->setTableName('login')
->setIdentityColumn('username')
->setCredentialColumn('password');
//->setCredentialTreatment('MD5(?)');
// pass to the adapter the submitted username and password
$authAdapter->setIdentity($username)->setCredential($password);
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($authAdapter);
if ($result->isValid()) {
// success : store database row to auth's storage system
// (not the password though!)
$data = $authAdapter->getResultRowObject(null, 'password');
$auth->getStorage()->write($data);
//$this->_redirect('/');
return $this->_helper->redirector('index','index');
} else {
// failure: clear database row from session
$this->view->message = 'Login failed.';
return $this->render('index');
}
return $result;
}
Have you read the documentation for Zend_Auth_Adapter_DbTable? - Yes, but i'm not sure if it cleared or muddied my understanding.
Do you have a valid connection to the database? - Yup, have a working CRUD app with the same DB config.
What is the structure of your users table? -
Did you specify the identity and credential columns correctly to the constructor of Zend_Auth_Adapter_DbTable? - See code above, i believe so.
Did you also specify the username/password you're trying to authenticate? - Yup, have a Zend form submitting these variables.
What is the SQL query that is causing the error? I think this is it.
SELECT `isp_partners_view`.*, (CASE WHEN `password` = '*****' THEN 1 ELSE 0 END) AS `zend_auth_credential_match` FROM `isp_partners_view` WHERE (`username` = '*****')
You have given the error, but there are number of other questions I'd ask to try to troubleshoot this:
Have you read the documentation for Zend_Auth_Adapter_DbTable?
Do you have a valid connection to the database?
What is the structure of your users table?
Did you specify the identity and credential columns correctly to the constructor of Zend_Auth_Adapter_DbTable?
Did you also specify the username/password you're trying to authenticate?
What is the SQL query that is causing the error? You can view the query this way:
$select = $authAdapter->getDbSelect();
print $select . "\n";