ZF2 get another navigation in global.php - php

This is my navigation in global.php
'navigation' => array(
'default' => array(
'loja' => array(
'label' => 'Loja',
'route' => 'loja',
'params' => array('action'=>'index'),
'pages' => array(
'estoque' => array(
'label' => 'Estoque',
'params' => array('action'=>'index'),
'action'=>'index',
'id' => 'estoque',
'route' => 'estoque',
)),
),
'suport'=> array(
'test' => array(
'label' => 'Loja',
'route' => 'loja',
'params' => array('action'=>'index'),
'pages' => array(
'estoque' => array(
'label' => 'Estoque',
'params' => array('action'=>'index'),
'action'=>'index',
'id' => 'estoque',
'route' => 'estoque',
)),
),),
when I call navigation the 'default' comes, I want to call the navigation 'suport', how I can do it?
My code in layout.phtml ..
echo $this->navigation('Navigation')->menu()->setUlClass('nav dropdown-submenu')->renderMenu();
Thanks :)

First create a container:
<?php $container = $this->navigation()->findOneByLabel('support');?>
Read more about finding nodes here: http://framework.zend.com/manual/2.2/en/modules/zend.navigation.containers.html
Than use it:
<?php echo $this->navigation()->menu()->renderMenu($container);?>
(Put this in your view script.)
Read more about this in the docs: http://framework.zend.com/manual/2.2/en/modules/zend.navigation.view.helper.menu.html

Can You Try This in view script:
Create Container Like this
<?php $container = $this->navigation('support')->getContainer(); ?>
Echo the Container to use this one:
<?php echo $this->navigation($container); ?>

Create your own navigation factory by extending Zend\Navigation\Service\DefaultFactory
namespace Application\Navigation\Service;
use Zend\Navigation\Service\DefaultNavigationFactory;
class MyNavigation extends DefaultNavigationFactory
{
public function getName() {
return 'suport';
}
}
and in Module.php
public function getServiceConfig()
{
return array(
'invokables' => array(
'my_navigation' => 'Application\Navigation\Service\MyNavigation'
}
}
}
Now you can use your navigation echo $this->navigation('my_navigation')->menu()

Related

URL link for custom module in Prestashop 1.6

I am currently developing a custom module.
What I want is to have a nice URL, because right now it looks like this:
domain.com/flower-deliveries?city=Hamburg&id_country=1&country=Germany
I already added a new page to link to the custom module, the page name is flower-deliveries, but still I have the parameters that I have to "hide".
Instead, of that link above I would like a URL like this:
domain.com/flower-deliveries-1-Hamburg-Germany.html
I tried 2 methods, but none of them worked..
The first one, was to add a hookModuleRoutes in my controller, just like below:
public function hookModuleRoutes($params)
{
return array(
'module-vpages-dpage' => array(
'controller' => 'dpage',
'rule' => 'flower-deliveries{-:id_country}{-:country}{-:city}.html',
'keywords' => array(
'id_country' => array('regexp' => '[_a-zA-Z0-9_-]+', 'param' => 'id_country'),
'city' => array('regexp' => '[\w]+', 'param' => 'city'),
'country' => array('regexp' => '[\w]+', 'param' => 'country')
),
'params' => array(
'fc' => 'module',
'module' => 'vpages',
'controller' => 'dpage'
)
)
);
}
And then, in the controllers install:
$this->registerHook('moduleRoutes');
That didn't worked, so I tried to override the Dispatcher class, by adding a custom module route:
'module-vpages-dpage' => array(
'controller' => 'dpage',
'rule' => 'flower-deliveries{-:id_country}{-:country}{-:city}.html',
'keywords' => array(
'id_country' => array('regexp' => '[0-9]+', 'param' => 'id_country'),
'city' => array('regexp' => '[\w]+', 'param' => 'city'),
'country' => array('regexp' => '[\w]+', 'param' => 'country'),
),
'params' => array(
'fc' => 'module',
'module' => 'vpages',
'controller' => 'dpage'
)
),
When using that custom rule, the link http://domain.com/flower-deliveries?city=Hamburg&id_country=1&country=Germany was tranformed in http://domain.com/flower-deliveries?module_action=list and it didn't worked and was redirecting me to the first page.
Could some one tell me what am I doing wrong?
I've spent hours of reading how it should be done and it should be just like the ones above..
Thank you!
Revert all edits that you have done :).
Try this way:
For example, this is core module file rootofps/modules/vpages/vpages.php
class VPages extends Module {
public function __construct(){
$this->name = 'vpages';
$this->author = 'you';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->controllers = array('dpage');
parent::__construct();
}
// This is the function in your core module file (not in controller)
public function install(){
return parent::install() && $this->registerHook('moduleRoutes')
}
public function hookModuleRoutes($params){
$my_link = array(
'vpages' => array(
'controller' => 'dpage',
'rule' => 'flower-deliveries{-:id_country}{-:country}{-:city}.html',
'keywords' => array(
'id_country' => array('regexp' => '[0-9]+', 'param' => 'id_country'),
'country' => array('regexp' => '[\w]+', 'param' => 'country'),
'city' => array('regexp' => '[\w]+', 'param' => 'city'),
),
'params' => array(
'fc' => 'module',
'module' => 'vpages'
)
)
);
return $my_link;
}
}
Now the controller rootofps/modules/vpages/controllers/front/dpage.php
class VpagesDpageModuleFrontController extends ModuleFrontController {
public function init(){
parent::init();
$this->setTemplate('dapage.tpl');
}
}
And now the view rootofps/modules/vpages/views/templates/front/dpage.tpl
id_country = {$smarty.get.id_country}<br>
country = {$smarty.get.country}<br>
city={$smarty.get.city}<br>
This 'skeleton' works at 100% :), by the way, notice that if you give an url like this mydomain.com/flower-deliveries?id_country=1&country=italy&city=rome PrestaShop will not transform your url in a clearly url as you want.
But an url like this mydomain.com/flower-deliveries-2-italy-rome.html will be routes properly :)

Getting params from redirected request

I'm trying to redirect the user using this code:
return $this->redirect()->toRoute('application', array(
'controller' => 'Index',
'action' => 'connexion',
null,
array('e' => 'n'),
));
And getting from the layout the content of the e param this way:
$_REQUEST['e']
But doing that I don't catch anything. How do I to get it please?
Thanks in advance!
Matched Route params in view :
$this->getHelperPluginManager()
->getServiceLocator()
->get('Application')
->getMvcEvent()
->getRouteMatch()
->getParams()
Request Query/Post in view :
$this->getHelperPluginManager()
->getServiceLocator()
->get('Request')
->getQuery()->toArray()
$this->getHelperPluginManager()
->getServiceLocator()
->get('Request')
->getPost()->toArray()
As mentioned in the comments of your question, the way to go is: $this->params()->fromRoute(); mentioned by #Notuser. Will use it in a simple example for your use as you're passing the parameters to the view.
class ExampleController extends AbstractActionController
{
public function rerouteAction()
{
// Notice that 'param' is a route within our route.config.php and in there we
// define the controller and action, so we do not need to set the controller
// and action in the redirect. So param now points to paramAction of ExampleController.
return $this->redirect()->toRoute('param', array('e' => 'n'));
}
public function paramAction()
{
// Leaving fromRoute() blank will return all params!
$params = $this->params()->fromRoute();
$e = $params['e'];
return array('e' => $e);
}
}
So in your view.phtml you can now easily do <?php echo $this->e; ?> which should contain: n.
The route.config of the above example will look something like this:
return array(
'router' => array(
'routes' => array(
'reroute' => array(
'type' => 'segment',
'options' => array(
'route' => 'reroute',
'defaults' => array(
'controller' => 'Application\Controller\ExampleController',
'action' => 'reroute'
)
)
),
'param' => array(
'type' => 'segment',
'options' => array(
'route' => 'param',
'defaults' => array(
'controller' => 'Application\Controller\ExampleController',
'action' => 'param'
)
)
)
)
)
);

ZF2 dynamic routing

I have problem with the routing in ZF2. I want to make dynamic routing for the software, that I'm making.
For example:
This is the URL: http://localhost:8080/application/index.json/
And this is my module.config (router part):
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
),
),
),
'restful' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/:module/[:controller[/:action][.:formatter][/:id]]',
'constraints' => array(
'module' => '[a-zA-Z][a-zA-Z0-9_-]*',
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'formatter' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z0-9_-]*'
),
),
),
),
),
Everything is working fine, but when I create new controller, have to add it to controllers['invokables'] setting in module.config.
'controllers' => array(
'invokables' => array(
'index' => 'Application\Controller\IndexController',
'cloud' => 'Application\Controller\CloudController',
),
),
So the question is, how to automate the controllers['invokables'] to process requests dynamically, without describing every controller in it.
Fast and dirty, but you get the idea.
namespace Application;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
class Module
{
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach (MvcEvent::EVENT_ROUTE, function (MvcEvent $e) {
$controller_loader = $e->getApplication ()->getServiceManager ()->get ('ControllerLoader');
$controller = $e->getRouteMatch ()->getParam ('controller');
$controller_class = '\Application\Controller\\'.ucfirst ($controller).'Controller';
// Add service locator to the controller
$controller_object = new $controller_class;
$controller_object->setServiceLocator ($e->getApplication ()->getServiceManager ());
// ------------------------------------
$controller_loader->setService ($controller, $controller_object);
});
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}

Zend Framework2 : Post is not working correctly

I am a Zend Framework beginner.
I guess My question is very basic... but I can't solve it by myself.
In indexAction, $request->isPost() is always false.
What is happening?
EntryController::indexAction
public function indexAction() {
$form = new AgreementForm();
$form->get('submit')->setValue('Go Entry Form');
$request = $this->getRequest();
if ($request->isPost()) {
var_dump('//// $request->isPost() is true //////');
if ($form->get('agreementCheck')) {
// Redirect to list of entries
return $this->redirect()->toRoute('entry');
} else {
return array('form' => $form);
}
} else {
var_dump('//// $request->isPost() is false //////');
return array('form' => $form);
}
}
form in index.phtml
<?php
$form = $this->form;
$form->setAttribute('action', $this->url('entry', array('action' => 'index')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formCheckbox($form->get('agreementCheck'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
?>
AgreementForm is generated using code generator.
http://zend-form-generator.123easywebsites.com/formgen/create
as below.
class AgreementForm extends Form {
public function __construct($name = null) {
parent::__construct('');
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'agreementCheck',
'type' => 'Zend\Form\Element\MultiCheckbox',
'attributes' => array(
'required' => 'required',
'value' => '0',
),
'options' => array(
'label' => 'Checkboxes Label',
'value_options' => array(
'0' => 'Checkbox',
),
),
));
$this->add(array(
'name' => 'csrf',
'type' => 'Zend\Form\Element\Csrf',
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Go',
'id' => 'submitbutton',
),
));
}
}
Please tell me some hints.
update:
In the result of analysis by Developer Tools, POST and GET works at the same time.
update:
router definition #module.config.php is this.
'router' => array(
'routes' => array(
'entry' => array(
'type' => 'segment',
'options' => array(
'route' => '/entry[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Entry\Controller\Entry',
'action' => 'index',
),
),
),
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Entry\Controller\Entry',
'action' => 'index',
),
),
),
),
),
A few things are wrong:
In the form class you add a csrf element, but you don't render it in the view. This will cause a validation error. So you need to add this to your view:
echo $this->formHidden($form->get('csrf'));
You're adding a Multicheckbox element to the form, but in your view you're using the formCheckbox view helper to render it. If you really want a Multicheckbox then you should render it with the formMultiCheckbox helper:
echo $this->formMultiCheckbox($form->get('agreementCheck'));
After these changes it should work.
Edit: Also you may want to pass a name to the form constructor:
parent::__construct('agreementform');
I think, you do not need to explicitly say
$form->setAttribute('action', $this->url('entry', array('action' => 'index')));
Omit that line, and see what happens.

First optional routing segment acts as mandatory one for child routes

I have the following routing definition:
'admin_default' => array(
'type' => 'segment',
'options' => array(
'route' => '[/:lang]/administrator[/:module][/:action]',
'constraints' => array(
'lang' => '[a-zA-Z]{2}',
'module' => '[a-zA-Z0-9_-]*',
'action' => '[a-zA-Z0-9_-]*',
),
'defaults' => array(
'module' => 'Application',
'controller' => 'Admin',
'action' => 'index',
'lang' => 'ru'
),
),
'may_terminate' => true,
'child_routes' => array(
'wildcard' => array(
'type' => 'wildcard',
'may_terminate' => true,
'options' => array(
'key_value_delimiter' => '/',
'param_delimiter' => '/'
),
),
),
),
So, I can't get rid of segment [/:lang] in URL string
For example:
URL view helper $this->url('admin_default', array('module' => 'albums')) returns the following URL string:
/administrator/albums
while $this->url('admin_default/wildcard', array('module' => 'albums', 'action' => 'edit', 'id' => album_id_here)) returns:
/ru/administrator/albums/edit/id/album_id_here
How can I remove [/:lang] segment from URL string in second case?
so whats the matter with that "ru" ?
you have to extend the zend view helper URL to inject your current locale to the URL
look what i made for my current project :
<?php
namespace PatrickCore\View\Helper;
use Doctrine\ORM\EntityManager;
use Zend\View\Helper\Url;
class I18nUrl extends Url {
/**
* #var String
*/
protected $lang;
protected $router;
public function __construct($locale,$router) {
$arraylanguagemapping = array(
'en_US' => 'en',
'fa_IR' => 'fa'
);
$this->lang = $arraylanguagemapping[$locale];
$this->router = $router;
}
public function __invoke($name = null, array $params = array(), $options = array(), $reuseMatchedParams = false) {
$this->setRouter($this->router);
if (!array_key_exists('lang', $params)) {
$params['lang'] = $this->lang;
}
return parent::__invoke($name,$params,$options,$reuseMatchedParams);
}
}
?>

Categories