zend-framework2

ZF2: how do I get ServiceManager instance from inside the custom class

流过昼夜 提交于 2019-12-21 04:25:26
问题 I'm having trouble figuring out how to get ServiceManager instance from inside the custom class. Inside the controller it's easy: $this->getServiceLocator()->get('My\CustomLogger')->log(5, 'my message'); Now, I created a few independent classes and I need to retrieve Zend\Log instance inside that class. In zend framework v.1 I did it through static call: Zend_Registry::get('myCustomLogger'); How can I retrieve the My\CustomLogger in ZF2? 回答1: Make your custom class implement the

ZF2 - BjyAuthorize - How to Get Rules and Guards from a Database

本秂侑毒 提交于 2019-12-21 02:34:15
问题 I'm using BjyAuthorize with Zend Framework2 to implement authorization and was able to successfully integrate roles from database. Now I want to get my Rules and Guards also from data base tables. How can I do this? 回答1: The easiest method and "the trick" here is really to: Get your rules and guards into the same array format as it is shown in example configuration. So after reading records from the database, in whatever format your raw database data is, process it to match the same guard

Can I render view without returning it in action controller in zend framework 2?

若如初见. 提交于 2019-12-20 23:21:24
问题 Now I am doing so: public function indexAction() { ... $view = new ViewModel(array( 'foo' => 'bar', )); return $view; } The problem is that I want to do something after $view rendering and before layout rendering: public function indexAction() { ... $view = new ViewModel(array( 'foo' => 'bar', )); $layout = $this->layout(); $layout->content = $view->render(); ... // here I want to do some important action ... $html = $layout->render(); return $this->getResponse()->setContent($html); } But

Zend 2 db transactions?

删除回忆录丶 提交于 2019-12-20 23:20:39
问题 How do we use transactions in Zend 2? I didn't find anything in the API, and a couple questions for Zend 1 refered to the regular PDO functions, but I don't see anything like that in Zend 2. 回答1: Try this: $adapter = new Zend\Db\Adapter\Adapter(array( 'driver' => 'pdo', 'dsn' => 'mysql:dbname=db;hostname=localhost', 'username' => 'root', 'password' => 'password', 'driver_options' => array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'' ), )); $adapter->getDriver()->getConnection()-

How to add an external javascript file to a Zend Framework 2 application?

烈酒焚心 提交于 2019-12-20 18:31:46
问题 I need to add jQuery and other javascript files to my Zend Framework project. I am trying to do it with an Action controller:- public function userinfoAction() { $this->view->headScript()->appendFile($basePath .'/js/validate_jquary.js'); $this->headScript()->appendFile('http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'); return new ViewModel(); } But it is not working. 回答1: Here is how you can use view helpers from within a controller in ZF2 to solve your problem: public

Zend Framework 2 file download

 ̄綄美尐妖づ 提交于 2019-12-20 17:14:43
问题 I made a form to upload files to the folder ./data/uploads using the Zend\Filter\File\RenameUpload filter. This is working like a charm. My problem now is how do I provide this file to users download it? I think it would be something like: $response->setContent(file_get_contents('./data/uploads/file.png')); But I want to know what is the best way to do this. 回答1: Thanks to @henrik for response, but several important headers are missing in his answer. Be careful of that. Full headers stack:

zf2 doctrine2 and Zend\Db\Adapter\Adapter using one db connection

假如想象 提交于 2019-12-20 15:31:07
问题 I use doctrine2 with ZF2, some of my libraries work with Zend\Db\Adapter\Adapter, others with doctrine2. Now, they connect to database twice. Is it possible to use one db connection in doctrine and standard ZF2 db adapter? 回答1: The DoctrineORM module accepts a PDO resource or a service name where the instance can be located in the service manager instead of the usual connection params. First step is to create a service factory which retrieves the PDO resource from the Zend\Db\Adapter\Adapter

ZF2 Routing as in ZF1

被刻印的时光 ゝ 提交于 2019-12-20 10:27:36
问题 How can I make the routing automatically work for everything in the ZF1 structure? module/controller/action/par1Name/par1Val/par2Name/par2Val/ I read the information about routing, but the way I see it, I'd have to add all actions manually, and I see a problem with optional params... 回答1: You can set up a wildcard child_route, at least on a per-controller basis, to get zf1-like routes: 'products' => array( 'type' => 'Zend\Mvc\Router\Http\Segment', 'options' => array( 'route' => '/products[/

Move to https://www, when it is www or http:// or without www and http://?

落爺英雄遲暮 提交于 2019-12-20 07:48:55
问题 I am using Zend framework where i have nice looking url controllers. Following .htaccess is working but its making SEO to see us as four links for one page. RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L] RewriteCond %{HTTPS} !=on RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R] I need to do following fix using htaccess: www.stackoverflow.com

Calling a method in model from layout in Zendframework 2

爷,独闯天下 提交于 2019-12-20 07:18:55
问题 I try in Zendframework 2 to call a method in model form layout to show some user specific things. I have tried to do it in Module.php in init and onBootstrap and tried to declare some variables that will be available in layout.phtml, but I failed and have not found anything usefull. 回答1: You'd typically use a view helper as a proxy to your model for this Create a view helper in your application, eg., <?php namespace Application\View\Helper; use Zend\View\Helper\AbstractHelper; class