Zend Framework - Last Code to Execute Before Layout is Rendered - php

I want to execute some code right before the layout is rendered, after all other code is executed. Where would I put that code?
I am specifically trying to modify the files referenced in the headLink, headScript, and inlineScript view helpers before they're used by the layout.
Here are the steps I want to take:
Loop over the files in those view helpers
Make a list of the local files
Remove local files from the view helpers
Reference the local file list as a parameter to a server script that combines them for a single HTTP request
Add that new combine script reference to the appropriate view helper
It doesn't appear that a Front Controller Plugin is going to help me accomplish this, and here's why:
postDispatch() gets executed after every controller action that's executed, and I need the full list of stylesheets/javascripts
dispatchLoopShutdown() gets executed after the controller action loop, but the layout has already been rendered at this point
As Rufinus suggested, I solved this by extending the View Helpers. My question from that angle as well as the solution is here.

see Orginal PDF created by Thorsten Ruf
(Mirror)
the very last part you can access via plugin should be dispatchLoopShutdown
EDIT:
For ZendFramework2 see http://zendframework2.de/en/cheat-sheet.html
or this gdoc

Related

Automatically Include wp-load on Admin Plugin View Pages

I am building a Wordpress plugin that allows admins to edit store hours from the backend of Wordpress. When a user clicks a department for which to edit the hours, a separate view ('hours.php') loads via jQuery
.load()
from this view, the list of hours for that department is supposed to be called up and displayed to the user. The functions that complete these tasks are in a separate class file. The problem is that in order for this class to be called I have to include the wp-load.php file as well as the class file. I do not want to do this as my hours.php file looks like this:
include('../../../../wp-load.php');
include('../class.libhours-database.php');
I know that is bad practice and do not intend on keeping it that way.
I read this article on query_vars and parse_request but I don't think this is exactly what I am looking for as I am not passing a URL at any point.
Remember: this plugin is ONLY accessible by admins and is only done on the backend.
Wordpress always loads wp-load.php, so you don't have to worry about that.
If your PHP script that handles yor Ajax call from the load() needs a separate class to create the HTML to return, then having it include the necessary PHP file that contains the class is perfectly appropriate.

Switching from Procedural to OOP PHP using Code Igniter

I'm finally making the switch to OOP from procedural and starting with CodeIgniter and just looking for clarification. For every page that you would typically have (about.php) will you have both a view (about.php) and a controller (About.php) for every page? I'm trying to wrap my head around it and I've always given up and went back to procedural and now I'm forcing myself to learn object oriented.
That's certainly one way you could do it, but it doesn't have to be that way.
In CodeIgniter, your controller forms a section of your site, and looks in most URLs like a sub-folder, e.g.
http://example.net/products/tvs
There, products is the name of a controller, and tvs - in URL terms, the sub-page - is a method of that controller.
So yes, you could have a controller for each page, but no, you normally wouldn't. Think instead of controllers being responsible for site sections rather than individual pages.
The controller controls everything, you could have all your info in a DB, but if you want to have actual files, then put them in your "view" folder and just add a function in your controller.
Something like this:
controller:
function about_us_page() {// load your about_us.php view file here}
view:
about_us.php
and you should link to it like:
example.com/controller/about_us.php

Where to include js files ,in actions(controller) or view (zf2)?

Hi all im working on zf2, I was allways including js files from the view pages. But i can include it in the controller also using appendscript(). Which is the best method according to mvc ?
Javascript works with view output - the view knows, what javascript is needed to make everything work. And the view might not be directed to the browser, other view media might be used. For this reason, I prefere adding javascript from inside view.

Repeated array in parent template using Twig and Slim

I've just started using Slim and Twig and getting to grips with it all.
I am building a small site and will have a Twitter feed on every page. So, I want to put this in my layout template (base.html.twig). The only way I can see to do it is to pass it in to every route and then use an include that can access the details.
Seems like there must be a way to set it in the layout template once without passing it in through every route?
Any help or a related link would be great.
you can extend twig with user defined functions, just make a call inside template and you will not need for passing it every time http://twig.sensiolabs.org/doc/advanced.html#creating-an-extension

Yii framework custom headers

I want different headers for my landing page and all other pages in my webapp. What is the best way to do this?
AS far as my understanding goes, the header and footer are loaded from the view/layouts/main.php, In my case since I am using a theme, it is loaded from themes/new/views/layouts/main.php
Now I want the header section for all my pages except the landing page, to use the header section as defined in the "main.php", however the landing page should have a different header. How should I do this? Should I use a render partial? And since I am Yii newbie, how should I do this?
Can I also use one of the other layouts files, column1.php or column2.php? And how?
I am not looking for extensive hand holding here. I just need a heads up, as to how people have implemented similar functionality.
It partly depends on how different the headers are going to be in terms of your approach. If you want them to be completely separate, you could use additional layout files either in combination with main.php or instead of it.
You set the layout file at the start of the controller class with something like:
public $layout='//layouts/column2';
This will set the default layout for a controller. You can change the value in an action function or evaluate a condition in the "beforeAction" function.
The default generated admin pages (with "gii" or the command-line) utilize those column1.php and column2.php layouts with main.php and provide a decent example to see how they work. Just move the content out of main.php you want to customize and put it into separate layout files. If you still are sharing content, you can leave the shared content in main.php.
If it's just a matter of changing a few attributes, you can use $this->getAction()->getId() to get the action name and use that to change what content is loaded within the layout, e.g., a certain css or js file. Any complex logic you would want to do in the controller.
For something like a nav bar or similar, you could also use include or renderPartial base on an environment variable you set in the controller.
I'm usually create a widget like HeaderWidget with few view files and include in main layout. In controller or action define necessary view file of header and pass them into widget.
In base controller you can define property public $headerName = 'defaultHeaderView'
And set value depends of some conditions.
Of cource, you need to create BController extends CController and all other controllers extends from your BController

Categories