Unify ZF2 Translator and AngularJS localization - php

I built an application that uses ZF2 for authentification, routing, error pages etc., but the core functionalities on each view are implemented in AngularJS. The whole thing is localized, but in 2 seperate instances:
We have the ZF2 Translator, configured in module.config.php
'translator' => array(
'locale' => 'de_DE',
'translation_file_patterns' => array(
array(
'type' => 'phparray',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.php',
),
),
),
containing key=>value pairs like 'app.frontend.title' => 'Title'.
And the Angular-Translate module, configured by
$translateProvider.useStaticFilesLoader({
prefix: '/lang/',
suffix: '.json'
});
containing a nested JSON object like {'app': {'buttons': {'send': 'Send now'}}}'
The PHP part contains some headlines, content for <title>, navigation,... pretty much everything that is displayed outside of my AngularJS apps. The Angular-JSON contains l10n for a lot of buttons, dialogs etc.
Is there a possibility to unify these two? Doesn't matter if I access the AngularJS json file from the php script or the other way round (get the .json dynamically served by PHP for Angular). But I can't figure out how to read JSON for the ZF2 Translator.

Why would have you have some translations handled by PHP and some others by Angular JS? Couldn't you get rid of the ZF2 Translator?

Related

ZF2 empty output on underscore method

I've got an ZF2 application which is an extended skeletonapplication with a few extra modules like authentication and authorization, running on Zend Server comunity edition.
In order for poedit to find the strings it has to translate i use the _("string") method like so:
$this->add(array(
'name' => 'eaddress',
'type' => 'Zend\Form\Element\Text',
'attributes' => array(
'id' => 'eaddress',
'type' => 'email',
),
'options' => array(
'label' => _('EMAIL'),
),
));
But when I do that the application outputs nothing to the browser not even an error:
Firefox: The connection was reset
Chrome: No data received
Safari: {shows epmty page}
Does anyone know how to fix this?
Thanks in advance
As far as i know, you dont need to write any function in order to have ZF2 translating your literals. Actually it is working with poedit like a charm.
If you look at this manual page you will see how to easily translate stuff in your views, just using $this->translate(), and there is where actually you have to do it.
About forms labels, you dont even have to worry since the form view helpers already translate label if everything is well configured.
If you want to know more about poedit and zf2 you can go to this blog post or this great sams's post

Can't manage to create a static page with Zend Framework 2

I'm trying to make a website using the Zend Framework 2, but I have a simple problem driving me crazy.
I'd like to make a simple about-us.html page with static content because there is no need to do anything else than display html.
There is no need to create a Controller / model etc...
Maybe you have to question why you're using ZF in the first place. If you just want to create a static internet page, do that without a PHP framework!
If you didn't ask your question well and you're actually just adding a static page to an existing ZF application, why not just using the existing IndexController, add an empty action and add your static content to the corresponding .phtml?
Alternatively you can look at PhlySimplePage, a ZF2 module for the purpose of adding simple static pages to a ZF app. It's written by Matthew Weier O'Phinney, the lead dev of ZF.
As the readme says, this module eliminates the need to create a controller and an action. So all you need to do is create a route and a view script.
I know this question might be old, but I was in trouble with that as well.
Here is what I did to create a generic router for my application, so I could just add as many actions as I need and have then created as phtml files in my view
on module.config.php
'application' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:action][/]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
),
So basically what it does is gets any action you might have in your application and use as a page... like any other controller
Instead of http://yoursite.com/application/test
you can do now http://yoursite.com/test
if you have your testAction set in your indexController file.
Hope I had helped future people looking for this information.
You have to create controller and view file.
suppose there is a about us menu with link
<a href="<?php $this->url('aboutus'array('action'=>'aboutus'));?>">
About Us
</a>
Just go to aboutus view file and write static html code.
Thanks
Alok

Zend Translate custom language

Depending on the user role, I need to show different texts in my Zend project.
For normal users I'm using the "en" language.
For new users I want something like "en_new".
However, the language "en_new" always reverts to just "en".
I'm using the locale_directory scan system to automatically detect languages.
The translate adapter calls Zend_Locale::findLocale() internally in addTranslation() (at least in ZF 1.1x). This in turn checks whether the locale is on a whitelist. Yours is not, obviously. I didn't dig too deep into the code, but it's quite probable that the next step is to revert from en_xxx to just en which is what happens in your case.
See the sources:
library/Zend/Translate/Adapter.php - addTranslation method
library/Zend/Locale.php - findLocale method
I am currently evaluating something similar, for some users I want some texts to be differently translated. And I also ran into the problem to not be able to create a custom locale value.
Tough what I found out in my tests seems to solve/work around the problem.
See also here: Combining multiple translation sources
What I am doing is to just add a custom translation to my default ones.
$translateDef = new Zend_Translate(
array(
'adapter' => 'gettext',
'content' => 'locale/default/',
'locale' => 'auto',
'scan' => Zend_Translate::LOCALE_DIRECTORY
)
);
$translateCust = new Zend_Translate(
array(
'adapter' => 'gettext',
'content' => 'locale/custom/',
'locale' => 'auto',
'scan' => Zend_Translate::LOCALE_DIRECTORY
)
);
$translateDef->addTranslation(array(
'content' => $translateCust
)
);
And the folder structure looks like this:
locale/
default/
de
en
custom/
de
en
So when doing the addTranslation it seems to overvwrites the existing ones, so for your new users, you could add custom folder with the proper translations.
For my tests this worked so far, but haven't evaluated it in depth yet.

Zend_Navigation incompatible with Zend_Router when writing controller/actions?

Novice Zend Framework developer here trying to figure out a simple solution to a Zend Routing problem. I'm sure one of you pros can lend a hand.
I have a website (built in Zend Framework using Zend_Navigation) that contains 75% static HTML page content and a few controllers. Top level navigation is built in Zend_Navigation, looping through partials.
Because of my work I build a lot of sites along these lines (containing lots of static pages) so I want to get this right. I don't want to set up controllers and actions for each and every one of these static pages (there are many) and I wanted to create a solution where I used Zend_Controller_Router_Route to route all static content automatically through to a StaticController whose job it would be to include or render .phtml pages based on a controller/action pairing in the URL from some sort of directory like /layouts/staticpages
Because of SEO and various reasons I don't want to have the controller pairing in the URL for these static pages be visible as /static/page/page1... It has to be "real world descriptions" of the /section/page (eg. advantages/someadvantage )
Here is the problem: Using Zend_Controller_Router_Route can do the job when I set up the correct routes BUT it messes something awful with Zend Navigation... I assume because Zend_Navigaion doesn't play nice with on-the-fly controller/action switching.
Code example:
$router = Zend_Controller_Front::getInstance()->getRouter();
$route = new Zend_Controller_Router_Route('advantages/:page/*',
array('controller' => 'static', 'action' => 'display', 'mode' => 'advantages',
'page' => 'index'));
$router->addRoute('advantages', $route);
This handles the job of switching pages in the "advantages" section well enough, but Zend_Navigation's automatic controller/action writing AND the highlighting of "active" nodes ends up being all screwed up because it now thinks that its controller is "static" and action is "display".
Is Zend_Navigation fundamentally incompatible with Zend_Controller_Router_Route? Is there a better way of doing this single static page controller or handling static content across the board?
Since you are using one controller/action for all static pages, you must customize your Zend Navigation before displaying it.
Check Example 4 in the Zend Documentation.
// the following route is added to the ZF router
Zend_Controller_Front::getInstance()->getRouter()->addRoute(
'article_view', // route name
new Zend_Controller_Router_Route(
'a/:id',
array(
'module' => 'news',
'controller' => 'article',
'action' => 'view',
'id' => null
)
)
);
// a page is created with a 'route' option
$page = new Zend_Navigation_Page_Mvc(array(
'label' => 'A news article',
'route' => 'article_view',
'module' => 'news', // required for isActive(), see note above
'controller' => 'article', // required for isActive(), see note above
'action' => 'view', // required for isActive(), see note above
'params' => array('id' => 42)
));
// returns: /a/42
$page->getHref();

PHP Application URL Routing

So I'm writing a framework on which I want to base a few apps that I'm working on (the framework is there so I have an environment to work with, and a system that will let me, for example, use a single sign-on)
I want to make this framework, and the apps it has use a Resource Oriented Architecture.
Now, I want to create a URL routing class that is expandable by APP writers (and possibly also by CMS App users, but that's WAYYYY ahead in the future) and I'm trying to figure out the best way to do it by looking at how other apps do it.
I prefer to use reg ex over making my own format since it is common knowledge. I wrote a small class that I use which allows me to nest these reg ex routing tables. I use to use something similar that was implemented by inheritance but it didn't need inheritance so I rewrote it.
I do a reg ex on a key and map to my own control string. Take the below example. I visit /api/related/joe and my router class creates a new object ApiController and calls it's method relatedDocuments(array('tags' => 'joe'));
// the 12 strips the subdirectory my app is running in
$index = urldecode(substr($_SERVER["REQUEST_URI"], 12));
Route::process($index, array(
"#^api/related/(.*)$#Di" => "ApiController/relatedDocuments/tags",
"#^thread/(.*)/post$#Di" => "ThreadController/post/title",
"#^thread/(.*)/reply$#Di" => "ThreadController/reply/title",
"#^thread/(.*)$#Di" => "ThreadController/thread/title",
"#^ajax/tag/(.*)/(.*)$#Di" => "TagController/add/id/tags",
"#^ajax/reply/(.*)/post$#Di"=> "ThreadController/ajaxPost/id",
"#^ajax/reply/(.*)$#Di" => "ArticleController/newReply/id",
"#^ajax/toggle/(.*)$#Di" => "ApiController/toggle/toggle",
"#^$#Di" => "HomeController",
));
In order to keep errors down and simplicity up you can subdivide your table. This way you can put the routing table into the class that it controls. Taking the above example you can combine the three thread calls into a single one.
Route::process($index, array(
"#^api/related/(.*)$#Di" => "ApiController/relatedDocuments/tags",
"#^thread/(.*)$#Di" => "ThreadController/route/uri",
"#^ajax/tag/(.*)/(.*)$#Di" => "TagController/add/id/tags",
"#^ajax/reply/(.*)/post$#Di"=> "ThreadController/ajaxPost/id",
"#^ajax/reply/(.*)$#Di" => "ArticleController/newReply/id",
"#^ajax/toggle/(.*)$#Di" => "ApiController/toggle/toggle",
"#^$#Di" => "HomeController",
));
Then you define ThreadController::route to be like this.
function route($args) {
Route::process($args['uri'], array(
"#^(.*)/post$#Di" => "ThreadController/post/title",
"#^(.*)/reply$#Di" => "ThreadController/reply/title",
"#^(.*)$#Di" => "ThreadController/thread/title",
));
}
Also you can define whatever defaults you want for your routing string on the right. Just don't forget to document them or you will confuse people. I'm currently calling index if you don't include a function name on the right. Here is my current code. You may want to change it to handle errors how you like and or default actions.
Yet another framework? -- anyway...
The trick is with routing is to pass it all over to your routing controller.
You'd probably want to use something similar to what I've documented here:
http://www.hm2k.com/posts/friendly-urls
The second solution allows you to use URLs similar to Zend Framework.
Use a list of Regexs to match which object I should be using
For example
^/users/[\w-]+/bookmarks/(.+)/$
^/users/[\w-]+/bookmarks/$
^/users/[\w-]+/$
Pros: Nice and simple, lets me define routes directly
Cons: Would have to be ordered, not making it easy to add new things in (very error prone)
This is, afaik, how Django does it
I think a lot of frameworks use a combination of Apache's mod_rewrite and a front controller. With mod_rewrite, you can turn a URL like this: /people/get/3 into this:
index.php?controller=people&method=get&id=3. Index.php would implement your front controller which routes the page request based on the parameters given.
As you might expect, there are a lot of ways to do it.
For example, in Slim Framework , an example of the routing engine may be the folllowing (based on the pattern ${OBJECT}->${REQUEST METHOD}(${PATTERM}, ${CALLBACK}) ):
$app->get("/Home", function() {
print('Welcome to the home page');
}
$app->get('/Profile/:memberName', function($memberName) {
print( 'I\'m viewing ' . $memberName . '\'s profile.' );
}
$app->post('/ContactUs', function() {
print( 'This action will be fired only if a POST request will occure');
}
So, the initialized instance ($app) gets a method per request method (e.g. get, post, put, delete etc.) and gets a route as the first parameter and callback as the second.
The route can get tokens - which is "variable" that will change at runtime based on some data (such as member name, article id, organization location name or whatever - you know, just like in every routing controller).
Personally, I do like this way but I don't think it will be flexible enough for an advanced framework.
Since I'm working currently with ZF and Yii, I do have an example of a router I've created as part of a framework to a company I'm working for:
The route engine is based on regex (similar to #gradbot's one) but got a two-way conversation, so if a client of yours can't run mod_rewrite (in Apache) or add rewrite rules on his or her server, he or she can still use the traditional URLs with query string.
The file contains an array, each of it, each item is similar to this example:
$_FURLTEMPLATES['login'] = array(
'i' => array( // Input - how the router parse an incomming path into query string params
'pattern' => '#Members/Login/?#i',
'matches' => array( 'Application' => 'Members', 'Module' => 'Login' ),
),
'o' => array( // Output - how the router parse a query string into a route
'#Application=Members(&|&)Module=Login/?#' => 'Members/Login/'
)
);
You can also use more complex combinations, such as:
$_FURLTEMPLATES['article'] = array(
'i' => array(
'pattern' => '#CMS/Articles/([\d]+)/?#i',
'matches' => array( 'Application' => "CMS",
'Module' => 'Articles',
'Sector' => 'showArticle',
'ArticleID' => '$1' ),
),
'o' => array(
'#Application=CMS(&|&)Module=Articles(&|&)Sector=showArticle(&|&)ArticleID=([\d]+)#' => 'CMS/Articles/$4'
)
);
The bottom line, as I think, is that the possibilities are endless, it just depend on how complex you wish your framework to be and what you wish to do with it.
If it is, for example, just intended to be a web service or simple website wrapper - just go with Slim framework's style of writing - very easy and good-looking code.
However, if you wish to develop complex sites using it, I think regex is the solution.
Good luck! :)
You should check out Pux https://github.com/c9s/Pux
Here is the synopsis
<?php
require 'vendor/autoload.php'; // use PCRE patterns you need Pux\PatternCompiler class.
use Pux\Executor;
class ProductController {
public function listAction() {
return 'product list';
}
public function itemAction($id) {
return "product $id";
}
}
$mux = new Pux\Mux;
$mux->any('/product', ['ProductController','listAction']);
$mux->get('/product/:id', ['ProductController','itemAction'] , [
'require' => [ 'id' => '\d+', ],
'default' => [ 'id' => '1', ]
]);
$mux->post('/product/:id', ['ProductController','updateAction'] , [
'require' => [ 'id' => '\d+', ],
'default' => [ 'id' => '1', ]
]);
$mux->delete('/product/:id', ['ProductController','deleteAction'] , [
'require' => [ 'id' => '\d+', ],
'default' => [ 'id' => '1', ]
]);
$route = $mux->dispatch('/product/1');
Executor::execute($route);
Zend's MVC framework by default uses a structure like
/router/controller/action/key1/value1/key2/value2
where router is the router file (mapped via mod_rewrite, controller is from a controller action handler which is defined by a class that derives from Zend_Controller_Action and action references a method in the controller, named actionAction. The key/value pairs can go in any order and are available to the action method as an associative array.
I've used something similar in the past in my own code, and so far it's worked fairly well.
Try taking look at MVC pattern.
Zend Framework uses it for example, but also CakePHP, CodeIgniter, ...
Me personally don't like the MVC model, but it's most of the time implemented as "View for web" component.
The decision pretty much depends on preference...

Categories