Zend framework multiple routes for same controller - zend-framework

I'm trying to set up Zend pagination on my site so that I can use Paul Irish's jquery infinite scroll plugin, but I'm having trouble with my routes. I currently have these routes set up for my organizer page:
//Organizer searches
$route = new Zend_Controller_Router_Route('organizer/index/:filter/:page',
array('controller'=> 'organizer',
'action'=> 'index'));
$router->addRoute('organizer', $route);
$route = new Zend_Controller_Router_Route('organizer/index/:filter',
array('controller'=> 'organizer',
'action'=> 'index'));
$router->addRoute('organizer', $route);
It matches organizer/index/popular correctly in this order, but if I put a page number on it the filter suddenly comes up null. If I switch the order, organizer/index/popular/2 works perfectly fine but organizer/index/popular no longer works. I could just only use the more specific route since that's the one I'll need for pagination, but I would like to include both to accommodate users trying to type the url or in case I forget to change the links somewhere in my code. Can I incorporate multiple routes to the same controller with Zend? If so, what am I doing wrong?

You need to give the routes different names. You've called them both 'organizer', so the second one replaces the first each time.
You could also easily do this with one route simply by setting a default value for the page variable:
$route = new Zend_Controller_Router_Route(
'organizer/index/:filter/:page',
array(
'controller'=> 'organizer',
'action'=> 'index',
'page' => 1
)
);
$router->addRoute('organizer', $route);

Every route you add to the router must a have a unique name, so the second route you want to add must have a different name, because with your current code you overwrite the route organizer. Change the second call of $router->addRoute() to something like this:
$router->addRoute('organizer2', $route );

Related

Why are you able to redirect to a named route in Laminas, and insert a controller and an action?

In Laminas (Ex-Zend): Why are you able to redirect to a named and specified route (for example 'home') with the and give a controller and an action into the parameters, like this:
$this->redirect()->toRoute('home', ['controller'
=> 'NotHome', 'action' => 'displayAll])
Doesn't this the destroy the purpose of using a named route, if you just overwrite it with your own controller and action?
It won't redirect you directly to this controller, but to URL generated using the given route and params. If route home doesn't have a placeholder for controller param, especially is Literal and not Segment type, the parameter will have no effect on redirection result.

How to pass params in url to a backpack create form

I'm using backpack 3.3 on a laravel 5.5 installation.
I want to prefill two create form fields with two URL passed values.
Say you create a basic backpack crud, named Image.
Normally you can go to domain/admin/image/create to view and fill in the form.
But if I add one or two params in the url, I get a 404.
I guess I should manage the routes file (admin.php)
I tried this way:
Route::group(['prefix' => 'image/{k}/{cid}'], function()
{
CRUD::resource('image', 'ImageCrudController');
});
but still get a 404.
Any suggestions?
Thanks in advance.
Almost all field types have a default option. So when you define them, you can specify a default. And you can always pass a GET parameter for that default. So you can have something like this in your EntityCrudController:
$this->crud->addField[ // Text
'name' => 'title',
'label' => "Title",
'type' => 'text',
'default' => \Request::has('title')?\Request::has('title'):false, // default value
]);
You'd then be able to send you users to yourapp/admin/entity/create?title=your+default+value and have that default value show up in the field.
Hope it helps. Cheers!
it works for me easier
http://127.0.0.1:8000/admin/resultado/create?competencia=2
$this->crud->modifyField("competencia_id",[
'label' => "Competencia",
"default"=>$this->crud->request->query->get('competencia'),
.....

TYPO3 automatic page creation based on TCA record

I've special requirement on my project and I need help. I am using TYPO3 8.7.8. I've a custom extension to render tag labels in frontend. We can add the tags as TCA record in backend storage folder. In the TCA record, you can tag name. My requirement is, when I save the TCA record I want to create a TYPO3 page automatically with the same name as tag in a specific position. Everytime when I add a TCA record, I need to create corresponding page automatically. Is this possible? I can use hook while saving TCA. But is there any function to create pages automatically?
After automatic page creation, I want to insert a plugin content element in that page with a specific flexform value automatically. I know this is a strange requirement, but I would like to know if it is possible or not.
Exactly, you'd trigger a hook on saving and then as next step you can use the data handler to generate the new page (and possible content).
To create the page and content, use something like the following data structure
$data = [
'pages' => [
'NEW_1' => [
'pid' => 456,
'title' => 'Title for page 1',
],
],
'tt_content' => [
'NEW_123' => [
'pid' => 'NEW_1',
'header' => 'My content element',
],
],
];
Then call the datahandler with that structure:
$tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
$tce->stripslashes_values = 0;
$tce->start($data, []);
$tce->process_datamap();
Find out more in the docs at
https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Typo3CoreEngine/Database/Index.html#data-array
and
https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Typo3CoreEngine/UsingDataHandler/Index.html
Are you sure you need additional pages?
In general your problem sounds like you need one page where the plugin is inserted and where the plugin in dependency of an url-parameter (which can be converted with realurl into a path segment) shows only information depending of the selected record (tag).
If no tag is selected you can output a list with all available tags as a menu to navigate to all possible tags.
With a little effort (less than writing a hook like intended) you can add all tags to your menu.

Zend Framework + Tricky controller name and module name conflict

We have a Zend application that has these following modules:
Users
Shop
etc...
Front - A content management module
While the Front module has the following controllers:
UsersController
ShopController
AuthController
etc...
In the middle of our development cycle we decided to set the default module for the Zend application to the Front module, but inadvertently broke our links, as http://domain.com/front/users/list are now generated as http://domain.com/users/list, which is now pointing to the wrong action.
We are generating links using the URL view helper, (i.e. $this->url(array('module' => 'front', 'controller' => 'users', 'action' => 'list'));), but the 'front' URI segment is omitted since switching the default module to the Front module.
I totally understand why this is so, but we are avoiding renaming all controllers under the Front module to avoid conflicts.
My question is, is there is a way to instruct the URL view helper to always include the 'front' module URI segment even if it is already set as the default one?
My question is, is there is a way to instruct the URL view helper to
always include the 'front' module URI segment even if it is already
set as the default one?
You can create your own url view helper with same name and override default url view helper add it to Zend_View object in your bootstrap.
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
if (null === $viewRenderer->view)
{
$viewRenderer->initView();
}
$view = $viewRenderer->view;
$view->addHelperPath('/path/to/some/helpers', 'My_View_Helper');
Now create class My_View_Helper_Url let it extend Zend_View_Helper_Url override url method.
Here is reference form ZF doc about this procedure
In fact, you can "stack" paths using the addHelperPath() method. As
you add paths to the stack, Zend_View will look at the
most-recently-added path for the requested helper class. This allows
you to add to (or even override) the initial distribution of helpers
with your own custom helpers.
Having said that I think http://domain.com/users/list, should have worked correctly in first place since you have specified default module to front.

CakePHP Form in an Element Causing Errors

I think this may be an easy solution, but I've spent an hour now investigating to no avail.
I have a registration form in an element that is being used in views belonging to different controllers. Using the "url" attribute, I've told it to submit to /users/register, but for some reason, the fields aren't submitting to the database.
Instead, there are errors for "undefined index" and a MySQL error for an undefined secondary key that I set (it's empty because it's not being submitted). Strangely, the form works fine if I include the element somewhere in the users views. Does anyone know why this is happening?
UPDATE - Here's the relevant code, sorry:
<?php
echo $form->create(array(
'id' => 'signupform',
'url' => array(
'controller' => 'users',
'action' => 'register')));
?>
The form fields are all correct, since the element works in the user controller's views anywhere. Do I need to include any other information in the creation of the form to point it more directly?
Specify the model 'User' as the first parameter to the $form->create() method.
<?php
echo $form->create('User', array(
'id' => 'signupform',
'url' => array(
'controller' => 'users',
'action' => 'register')
)
);
?>
This will ensure that the form fields are named as data[User][field_name], and prevent you from seeing that undefined index error.
Hope this works for you!
Without seeing the code, it sounds like Cake is magically assuming that the Model is the one for the controller that controls the current view. When that controller is Users, it works correctly. When it is, say, Articles, it will be trying (and failing) to fit the form fields to the Article model.
Without seeing any code, it is impossible to offer any more help.
EDIT:
If the form contains mixed models, e.g. User and Article, you must prefix the fieldnames like this:
$form->input('User.username');
$form->input('Article.title');
etc.
If you don't, the controller will assume they all belong to its own model.