Here is my route:
'router' => array(
'routes' => array(
'upload' => array(
'type' => 'segment',
'options' => array(
'route' => '/products/upload[/:products]',
'defaults' => array(
'controller' => 'Products\Controller\Upload',
'action' => 'index'
),
),
'may_terminate' => true,
'child_routes' => array(
'uploadsuccessful' => array(
'type' => 'literal',
'options' => array(
'route' => '/uploadsuccessful',
'defaults' => array(
'controller' => 'Products\Controller\Upload',
'action' => 'successful'
),
),
),
),
),
),
);
I am trying to call this route several times from different view scripts giving different [/:products] parameter.
Upload Shoes Product Image
Upload Trainers Product Image
Upload Hat Product Image
Here is my Controller code.
<?php
namespace Products\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Products\Form\UploadForm;
class UploadController extends AbstractActionController
{
protected $_dir = null;
public function indexAction()
{
$products = $this->params()->fromRoute('products');
$config = $this->getServiceLocator()->get('Config');
$fileManagerDir =$config['file_manager']['dir'];
$this->_dir = realpath($fileManagerDir) .
DIRECTORY_SEPARATOR .
$products;
if (!is_dir($this->_dir)) {
//read, write, execute
mkdir($this->_dir, 0777);
}
$form = new UploadForm($this->_dir, 'upload-form');
$request = $this->getRequest();
if ($request->isPost()) {
$post = array_merge_recursive(
$request->getPost()->toArray(),
$request->getFiles()->toArray()
);
$form->setData($post);
if ($form->isValid()) {
$data = $form->getData();
$this->setFileNames($data);
return $this->redirect()->toRoute('upload/uploadsuccessful', array('products' =>$products));
}
}
return new ViewModel(array('form' => $form));
}
public function successfulAction()
{
$file = array();
$flashMessenger = $this->flashMessenger();
if ($flashMessenger->hasMessages()) {
foreach($flashMessenger->getMessages() as $key => $value) {
$file = $value;
}
}
return new ViewModel(array('file' => $file));
}
protected function setFileNames($data)
{
unset($data['submit']);
foreach ($data['image-file'] as $key => $file) {
rename($file['tmp_name'], $this->_dir . DIRECTORY_SEPARATOR . $file['name']);
}
}
}
I think the idea is clear: for each [/:products] parameter I tried to make separate folder with given name in $fileManagerDir.
But, there is a problem. When I click on button upload ($request->isPost() == true) parameter $products becomes null and uploaded files don't go to appropriate folders. Also I am not able to redirect to successful action - the error appears "missing parameter" because $products is null.
In your view script the upload buttons are href links. This will result in a GET REQUEST being sent to the controller and not a POST and no FILES will be present either. Unless that is, you are intercepting the click event with external JS which you haven't mentioned here. The route url's should be in the form action attribute. The submit buttons should be buttons not links (unless you are using JS not mentioned here). Make sure the form method is set to POST.
Something like:
<form action="<?php echo $this->url('upload', array('products' =>'shoes')); ?>" method="post" enctype="multipart/form-data">
You'll need to deal with the dynamic action using JS or redesign your routing.
Related
I've got a problem. I'm using zfcuser in my project, and now i want to create profile pages. I'have created the showAction() in UserController, but when i put the url to webbrowser, i will got 404.
public function showAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('zfcuser', array(
'action' => 'register'
));
}
try {
$user = $this->zfcUserAuthentication()->getIdentity()->getUser($id);
}
catch (\Exception $ex) {
return $this->redirect()->toRoute('zfcuser', array(
'action' => 'register'
));
}
return new ViewModel(array(
'user' => $user));
}
I also created custom function to get User info all in one row.
It looks like this:
public function getUser($id){
$rowset = $this->tableGateway->select(array('user_id' => $id));
$row = $rowset->current();
return $row;
}
And it is in Entity/User.php
This is my showAction() in zfc UserController.php
I thought its wrong module.config, but its not working anyway. I changed my module.config to this:
'show' => array(
'type' => 'Literal',
'options' => array(
'route' => '/show[/][:id]',
'defaults' => array(
'controller' => 'zfcuser',
'action' => 'show',
),
),
),
My problem is, i can not even get to url to show this controller action.
Where can be problem. I want to make simple user profile page.
The problem is in my opinion your route 'Literal'. A literal route can't have params.
You have to use Segment instead
But it's not the only problem i think.
ZfcUser comes with a packaging, profile page already exist :
Take a look to this complete slideshare :
Zf2 Zfc-User
I have controller with an add action.
public function add() {
$this->layout = 'manage';
$this->set($this->Restaurant->fetchRelatedData());
if ($this->request->is('post')) {
$this->Restaurant->create();
if ($this->Restaurant->save($this->request->data)) {
$this->Session->setFlash('ok!');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Error!');
}
}
}
View for this action created with Form and Js helpers:
echo $this->Form->create('Restaurant');
// some fields
echo $this->Form->input('district_id', array('label' => 'District'));
echo $this->Form->input('street_id', array('label' => 'Street'));
// other fields
echo $this->Form->end(array('label' => 'Add'));
$this->Js->get('#RestaurantDistrictId')->event('change',
$this->Js->request(array(
'controller'=>'streets',
'action'=>'getByDistrict'
), array(
'update'=>'#RestaurantStreetId',
'async' => true,
'method' => 'post',
'dataExpression'=>true,
'data'=> $this->Js->serializeForm(array(
'isForm' => true,
'inline' => true
))
))
);
Js helper shows list with streets that are in choosen district.
StreetsController -> getByDistrict action:
public function getByDistrict(){
$district_id = $this->request->data['Restaurant']['district_id'];
$streets = $this->Street->find('list', array(
'conditions' => array('Street.district_id' => $district_id),
'fields' => array('street'),
'order' => array('Street'),
'recursive' => -1,
));
$this->set('streets', $streets);
$this->layout = 'ajax';
}
Everything worked fine until I added the administrative prefix to this action.
If action is named public function add() – all things works.
If action is named *public function admin_add()* – Js helper stops updating streets list on district change.
I'm not 100% sure on this, but I believe that the JS helper is preserving the admin prefix when it makes the AJAX request. When in add() it would call getByDistrict(). When in admin_add() it would call admin_getByDistrict(). Try passing 'admin' => false into Js->request.
I want ask you about this problem.
What do i need to create Dynamic Menu with Zend\Navigation\Navigation?
In ZF1 i made like this:
$container = new Zend_Navigation();
$pages = array(
array(
'label' => 'Save',
'action' => 'save',
),
array(
'label' => 'Delete',
'action' => 'delete',
),
);
// add two pages
$container->addPages($pages);
and then in view:
$this->navigation()->menu();
But in ZF2 pages are taking from config.
Now i create \config\autoload\nav.global.php and here create page array. But i need to do page array in method and send it into navigation helper, but ii dont know how ((
i tried to do this in my controller:
use Zend\Navigation\Navigation;
$pages =array(
// All navigation-related configuration is collected in the 'navigation' key
'navigation' => array(
// The DefaultNavigationFactory we configured in (1) uses 'default' as the sitemap key
'default' => array(
// And finally, here is where we define our page hierarchy
'account' => array(
'label' => 'faq',
'route' => 'faq',
'pages' => array(
'news' => array(
'label' => 'news',
'route' => 'news',
),
'manual' => array(
'label' => 'manual',
'route' => 'manual',
),
),
),
),
),
);
$Menu = new Navigation($pages);
and then this in view:
$this->Menu()->menu();
but i have a lot of mistakes...
i think you understand my problem.
please help.
sorry for my english.
Follow these easy steps to create the multilevel menu dynamically Step
1:create a partial menu.phtml file and save it in layout folder or the
module,eg application/view/layout/menu.phtml
<ul id="menu" >
<?php foreach ($this->container as $page): ?>
<li <?= $page->isActive()? 'class="active"' : 'class="drop"' ?>>
<?php echo $this->navigation()->menu()->htmlify($page). PHP_EOL ?>
<div class="dropdown_2columns" >
<?php foreach ($page as $catpage) :?>
<div class="col_1" >
<h3 <?= $page->isActive()? 'class="active"' : 'class="drop"' ?> >
<?= $this->navigation()->menu()->htmlify($catpage). PHP_EOL ?>
</h3>
<ul >
<?php foreach ($catpage as $subpage) :?>
<li <?= $subpage->isActive()? 'class="active"' : 'class="drop"' ?>>
<?= $this->navigation()->menu()->htmlify($subpage). PHP_EOL ?>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
</div><!-- End dropdown container -->
</li>
<?php endforeach; ?>
</ul>
Step 2 in your module.php
public function getServiceConfig()
{
return array(
'initializers' => array(
function ($instance, $sm) {
if ($instance instanceof \Zend\Db\Adapter\AdapterAwareInterface) {
$instance->setDbAdapter($sm->get('Zend\Db\Adapter\Adapter'));
}
}
),
'invokables' => array(
'menu' => 'Application\Model\MenuTable',
),
'factories' => array(
'Navigation' => 'Application\Navigation\YourNavigationFactory'
)
);
}
in src/navigation folder in your module create YourNavigation.php and
YourNavigationFactory.php
namespace Application\Navigation;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Navigation\Service\DefaultNavigationFactory;
use Admin\Model\Entity\Tablepages;
class YourNavigation extends DefaultNavigationFactory
{
protected function getPages(ServiceLocatorInterface $serviceLocator)
{
if (null === $this->pages) {
$fetchMenu = $serviceLocator->get('menu')->fetchAll();
$configuration['navigation'][$this->getName()] = array();
foreach($fetchMenu as $key=>$row)
{
$subMenu = $serviceLocator->get('menu')->fetchAllSubMenus($row['id']);
if($subMenu){
$pages = array();
foreach($subMenu as $k=>$v)
{
foreach($v as $field=>$value){
$page['label'] =$value['heading'];
$page['route'] = 'visas';
if ($value['path'] == $row['path']){
$page['params'] = array('action'=>'index',
'category'=> $this->$row['path'],
);
}
$subCatMenu = $serviceLocator->get('menu')->fetchAllSubCatMenus($value['id']);
$subcatpages = array();
$subcatgroup = array();
$group = array();
if($subCatMenu>0){
foreach($subCatMenu as $k=>$v)
{
foreach($v as $field=>$value1){
$subpage['label'] =$value1['heading'];
$subpage['route'] = 'visas';
if ($value['path'] ==$row['path']){
$subpage['params'] = array('action'=>'index',
'category'=> $row['path'],
'sub_category'=> $value1['path']);
}elseif($row['id'] ==76){
$subpage['params'] = array('action'=>'index',
'category'=>$value['path'],
'sub_category'=>$value1['path']);
}else{
$subpage['params'] = array('action'=>'index',
'category'=> $row['path'],
'sub_category'=> $value['path'],
'id'=> $value1['path']);
}
}
$group[] =$subpage;
}
$page['pages'] =$group;
$pages[] =$page;
}
}
}
}
$configuration['navigation'][$this->getName()][$row['name']] = array(
'label' => $row['name'],
'route' => 'visas',
'params' => array(
'action' => 'index',
'category' => $row['path'],
),
'pages' => $pages,
);
}
if (!isset($configuration['navigation'])) {
throw new Exception\InvalidArgumentException('Could not find navigation configuration key');
}
if (!isset($configuration['navigation'][$this->getName()])) {
throw new Exception\InvalidArgumentException(sprintf(
'Failed to find a navigation container by the name "%s"',
$this->getName()
));
}
$application = $serviceLocator->get('Application');
$routeMatch = $application->getMvcEvent()->getRouteMatch();
$router = $application->getMvcEvent()->getRouter();
$pages = $this->getPagesFromConfig($configuration['navigation'][$this->getName()]);
$this->pages = $this->injectComponents($pages, $routeMatch, $router);
}
return $this->pages;
}
}
YourNavigationFactory.php
namespace Application\Navigation;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class YourNavigationFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$navigation = new MyNavigation();
return $navigation->createService($serviceLocator);
}
}
in your layout.phtml
<?php echo $this->navigation('navigation')->menu()->setPartial('menu')->render(); ?>
to create dynamic sitemap from navigation
$this->navigation('navigation')
->sitemap()
->setUseXmlDeclaration(false)
->setServerUrl('http://www.yourdomain.com')
->setFormatOutput(true);?>
echo $this->navigation()->menu()->setMinDepth(null)->setMaxDepth(null)->setOnlyActiveBranch(false)->setRenderInvisible(true);
to create breadcrumbs
echo $this->navigation()
->breadcrumbs()
->setLinkLast(true)
->setMaxDepth(1)
->setSeparator(' ▶' . PHP_EOL);
I Hope it helps you to save your time
For some backgrounds you might read this answer on another, but similar question regarding Zend\Navigation. The point is you want MVC pages and MVC pages in Zend Framework 2 need a way to assemble the url and find our if the url is active or not.
Every MVC page has a route name. The route stack routes the request and gets a route match. You must inject this route match into the navigation, so every page could check its own route against the matched one.
Similar for the url assembly. If you want to convert the route name into an url, you need the route stack ('router'). Inject the router in your application too and you are able to assemble.
In short:
use Zend\Navigation\Service\ConstructedNavigationFactory;
class MyController extends AbstractActionController
{
public function indexAction()
{
$config = array(
// your config here
);
$factory = new ConstructedNavigationFactory($config);
$navigation = $factory->createService($this->getServiceLocator());
return new ViewModel(array(
'navigation' => $navigation;
));
}
}
And similar as above answer, in your view:
<?php echo $this->navigation($navigation)->menu()?>
My previous answer is not correct. The following code works. For one page. In controller, edit action:
$page = new \Zend\Navigation\Page\Mvc(array(
'route' => 'application/default',
'controller' => 'album',
'action' => 'edit',
'use_route_match' => true,
));
$r = $this->getEvent()->getRouter();
$rm = $this->getEvent()->getRouteMatch();
$page->setRouter($r);
$page->setRouteMatch($rm);
echo $page->isActive() ? 'true' : 'false'; // true
echo $page->getHref(); // /test_app/public/application/album/edit/id1
You need to do so
In the controller
$pages = new \Zend\Navigation\Page\Mvc(array(
'pages'=>
array(
'album' => array(
'label' => 'Album3',
'controller' => 'album',
'action' => 'edit',
'params' => array('id'=>2),
'route' => 'album/default',
)
)
));
$navigation = new \Zend\Navigation\Navigation();
$serviceLocator = $this->getServiceLocator()->get('Application');
$routeMatch = $serviceLocator->getMvcEvent()->getRouteMatch();
$router = $serviceLocator->getMvcEvent()->getRouter();
$pages->setRouteMatch($routeMatch);
$pages->setDefaultRouter($router);
$navigation->addPage($pages);
In view
<?php echo $this->navigation($this->navigation)->menu() ?>
Eremite answer is not really wrong. I have tested all the recommendations given here and actually when the default route have several child_routes mark as default, menus doesn't mark the active flag correctly. So it is needed to pass the matched route as a parameter. But perhaps I'm doing something wrong.
Cheers
This function sets up the Magento Grid to display a list of filenames with a corresponding 'Delete' action.
The problem is the Delete action never passes the parameter, 'filename.' (See http://www.premasolutions.com/content/magento-manage-category-product-grid-edit-link) I have TESTDUMP for verification but it never prints on the next page.
Is 'params' a legitimate action of 'addColumn->actions->url'?
Update: Added the construct and prepare collection for controller. Maybe it's because of the type of Collection I'm using?
class Rogue_Googlemerchant_Block_Adminhtml_Exporter_Grid
Rogue_Googlemerchant_Block_Adminhtml_Exporter_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('googlemerchantGrid');
// This is the primary key of the database
$this->setDefaultSort('filename');
$this->setDefaultDir('ASC');
$this->setSaveParametersInSession(true);
}
protected function _prepareCollection()
{
$basePath = Mage::getBaseDir('base');
$feedPath = $basePath . '/opt/googlemerchant/';
$errPath = $basePath . '/var/log/googlemerchant/';
$flocal = new Varien_Io_File();
$flocal->open(array('path' => $feedPath));
$dataCollection = new Varien_Data_Collection();
foreach ($flocal->ls() as $item) {
$dataObject = new Varien_Object();
$dataObject->addData(
array(
'filename' => $item['text'],
'size' => $item['size'] / 1000 . ' kb',
'date_modified'=> $item['mod_date']
)
);
$dataCollection->addItem($dataObject);
}
$this->setCollection($dataCollection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('filename', array(
'header' => Mage::helper('googlemerchant')->__('File'),
'align' =>'left',
'index' => 'filename',
'width' => '200px',
));
$this->addColumn('action', array(
'header' => Mage::helper('googlemerchant')->__('Action'),
'width' => '50px',
'type' => 'action',
// 'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('googlemerchant')->__('Delete'),
'url' =>
array(
'base' => '*/*/delete',
'params' => array('filename' => 'TESTDUMP')
),
'field' => 'filename'
)
),
'filter' => false,
'sortable' => false,
// 'index' => 'filename',
// 'is_system' => true,
));
}
}
class Rogue_Googlemerchant_Adminhtml_ExporterController
class Rogue_Googlemerchant_Adminhtml_ExporterController extends Mage_Adminhtml_Controller_Action
{
public function deleteAction()
{
$filename = $this->getRequest()->getParam('filename');
$basePath = Mage::getBaseDir('base');
$feedPath = $basePath . '/opt/googlemerchant/';
$errPath = $basePath . '/var/log/googlemerchant/';
$flocal = new Varien_Io_File();
$flocal->open(array('path' => $feedPath));
d($filename);
if ($filename) {
try{
$flocal->rm($filename);
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('googlemerchant')->__('The file has been deleted.'));
$this->_redirect('*/*/');
}
catch (Mage_Core_Exception $e) {
$this->log($e);
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
$this->_redirect('*/*/index');
return;
}
}
die('here');
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Unable to find the file to delete.'));
$this->_redirect('*/*/');
}
}
The getter of the action column is used on the collection items to retrieve the argument value for the field parameter.
I'm not sure why you are specifying the filename hardcoded or if that should work, but if you add the column configuration
'getter' => 'getFilename'
and remove the params from the action it should work.
I want to add a new form to the edit customer page, so far so good, using rewrites to customer_edit_tabs i was able to add a tab and my admin form to the page. Code looks like this.
protected function _beforeToHtml()
{
$this->addTab('extraoptions', array(
'label' => Mage::helper('customer')->__('Extra options'),
'class' => 'ajax',
'url' => $this->getUrl('module/adminhtml_tabs/info', array('_current' => true)),
));
This adds my tab corrently. From there the link on the tabs controller:
public function infoAction()
{
$this->_init();
$this->getResponse()->setBody(
$this->getLayout()->createBlock('module/adminhtml_tabs_edit')->toHtml()
);;
}
This links to my form container on Block/Adminhtml/Tabs/Edit.php
class Namespace_Module_Block_Adminhtml_Tabs_Edit extends Mage_Adminhtml_Block_Widget_Form_Container{public function __construct()
{
parent::__construct();
$this->_objectId = 'id';
$this->_mode = 'edit';
$this->_blockGroup = 'module';
$this->_controller = 'adminhtml_tabs';
$this->_updateButton('save', 'label', Mage::helper('module')->__('Save'));
}
public function getHeaderText()
{
return Mage::helper('module')->__('Extra Options');
}
}
My Block/Adminhtml/Tabs/Edit/Form.php
class Namespace_Module_Block_Adminhtml_Tabs_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
public function __construct()
{
parent::__construct();
}
protected function _prepareForm()
{
$form = new Varien_Data_Form(array(
'id' => 'info_form',
'action' => $this->getUrl('module/adminhtml_tabs/save', array('id' => $this->getRequest()->getParam('id'))),
'method' => 'post',
'enctype' => 'multipart/form-data'
)
);
$fieldset = $form->addFieldset('extra_options', array('legend' => Mage::helper('module')->__('Extra Options Fieldset')));
$fieldset2->addField('extra', 'text', array(
'name' => 'zip',
'title' => Mage::helper('module')->__('extra'),
'label' => Mage::helper('module')->__('extra data'),
'maxlength' => '250',
'required' => false,
));
$form->setUseContainer(true);
}
protected function _prepareLayout()
{
return parent::_prepareLayout();
}
Everything is fine, I have a new button below the default save customer buttons, but this save button does not update the action, so if i click it, it goes to the default customer/edit/save action, it does not tell me the method does not exist which it should. My guess is that there is something wrong with the container but i have tried three tutorials with little differences to no avail, hope someone can help and even maybe someone will find my code helpful.
In this line of code:
'action' => $this->getUrl('module/adminhtml_tabs/save')
You are telling Magento to look for a module named module, a controller aliased adminhtml_tabs, and a saveAction() method within that file.
You need to figure out where you want to send the user when a save needs to be performed, and then place it there (e.g. the route to your controller->saveAction() method).
I decided to create a new button to save with a custom action. On the container:
$this->_addButton('save', array(
'label' => Mage::helper('adminhtml')->__('Save Extras'),
'onclick' => 'document.myform.submit();',
'class' => 'save',
),-1,5);
This did the trick.