Problems with Routes (Symfony 5.0.11) - php

I'm a beginner in Symfony and am using v5.0.11 as part of a work project (inb4: as its a work for the company I'm employed by, I have little control over updating the version).
My issue is the following: I have a controller (that extends the "CommonController") file that contains different routes leading to different URLs, supposed to be triggered by a multi-field form's button. The first route in this controller (except for the one leading to the main page) is "#Route: /global/search/" related to a "globalSearch funtion". The following routes (eg #Route: /global/product_code/{product_code}) are defined further down the document, indented at the same level and are located outside of that first "globalSearch" function.
Depending on the form's fields fill beforehand; it should send the user to a specific URL related to said field (eg: if I fill the "product code" category, it should send me to an address like "/global/product_code/{product_code}".
However, when I fill any field and press the button, it still sends me to the general search URL ("global/search") instead of the one I routed above '"global/product_code" for example.
Do you have any idea why that is ? I've gone down Symfony & Doctrine docs but couldn't find any answer for it; and from what I read I can't prioritize routes as I'm in 5.0.11.
Thanks in advance for any help.
NB: as my work concerns a sector and company bound by secret to my country's governement & multiple firms; I can't share the code online to 3rd parties; tho I will try to bring as many infos about the code as needed.

Without some code samples it would be hard to identify the problem and whether it's a problem at all.
But here are some ideas, how to narrow the problem down.
First:
However, when I fill any field and press the button, it still sends me to the general search URL ("global/search") instead of the one I routed above '"global/product_code" for example.
If you're using symfony forms to create and/or render forms, it's probably <form action=""> attribute. I assume it either blank (which will submit it to the same URL. In your case it's /global/search) or it is set to a specific route. (e.g /global/search )
In any case, you should consider #Cerads suggestion to check all available routes to find out more and find out which controller action is responsible for /global/product_code/{product_code}. If there are some routes matching this pattern, they have to have a "route name" so you could search in the whole project for that route name.
bin/console debug:router or bin/console debug:router | grep global
Second:
Depending on the form's fields fill beforehand; it should send the user to a specific URL related to
To somehow change submit-url by users-input requires JavaScript. Whether you send a single field value via AJAX or just "replace" form's action attribute, you have to check related JavaScript code.

Related

PHP MVC Form Submission

I have a question related to form submission done in PHP application that's built in MVC architecture (self-written framework).
All examples that I've seen so far (including existing back-end frameworks) work this way that once form for adding record to database is submitted then certain method of controller is executed [say i.e. addRecord()], which triggers method of appropriate model. If everything goes OK then record is added and controller's method [addRecord() in this example] renders view of "index" page that displays table with records from database.
What I would like to achieve is to render view with form used to add records (the same that I used to add first record) instead of "index". Obviously I can do it easily by just rendering appropriate view from addRecord() (view with the form).
But the tricky point is when you check url you'll see the following:
The first time you enter it will be i.e.
http://project_name/my_controller/create
Once first form was submietted and you return to the view from addRecord() method then url will be:
http://project_name/my_controller/addRecord
What I would like to see is return to the original url, that is http://project_name/my_controller/create
Not sure if this is clear?
PS. Of course I could use AJAX call for form submission (that way I will stay at the same page) but perhaps it's possible to achieve the same without AJAX.
Thanks in advance,
On the controller you will want to submit to the addRecord route and do the processing. Have a check to make sure it was successful and on successful submission you can redirect back to the create route.
It is hard to give an example since you are using a custom made framework. I use slim which has a redirect method for a route. If what you have made does not have something like that then using should do the trick.
header('Location: '.$createUrl);
die(); //or exit

HMVC internal/external requests and routes?

I think I understand the concept of HMVC after reading this question and answer https://softwareengineering.stackexchange.com/questions/220480/hmvc-and-database-connections; an extract from an answer is below:
Let's assume you want to have a view that enables a user to make a
comment to a blog post. You would have fields for name, e-mail, title
and comment, but you also want to have a field country displayed as a
dropdown. In the action that displays this view you would make a
database query that loads the countries and then populate that
dropdown. Which is ok, but it forces you to duplicate the query and
the view required to display the countries if you need it in another
part of your application. A better approach would be to create
separate controller for countries with an action that returns a view
with the dropdown and then render that action whenever you need to
show a list of countries.
What I cannot wrap my head around is that if I can internally request a controller/model/view which just displays a widget (e.g. a country select box), doesn't that mean that by accessing that url from a browser will also just show that view?
How is this managed in HMVC, are routes defined as internal/external only, so matching an internal route with an external request would show a 404 page?
Is this generally how it is done and is the HMVC description/definition above satisfiable with the general use case of it in most web applications?
Showing the output of a sub-request in the browser shouldn't be a problem, so I wouldn't bother, especially that those URLs are not known by the user and it's safe to output the widgets separately.
Despite the above, you could, as #deceze mentionned, not attach those controllers to any routes. If you have a "default" route (matching all requests), then you would have to disable it.

Trying to add a second view in a new component in Joomla! 3.x

I'm developing a component for Joomla! 3.x and I came across a strange issue. I followed the official documentation (http://docs.joomla.org/J3.2:Developing_a_MVC_Component/Adding_backend_actions) and I was able to get somewhere. Now the problem is that I wanted to expand the tutorial and create submenus within the components menu in the backend. I succeeded with this too.
The 2 submenu selections link correctly to 2 different views and I am able to fetch data from different tables nicely. The problem is that I cant add new entry to the database using my second view. The first view works fine. On the second view, when I click the green Add button, I get a jquery error: Uncaught TypeError: Cannot read property 'task' of null
The problem is that the addNew method cant find municipality.add or something. However this (almost) same code works for the default view.
What I'm trying to do is to display the data of 2 different tables in the DB and then being able to edit delete or add new.
Any ideas? Thanks in advance
The code
municipality.add
and
municipalitys.delete
refers to two different controllers, named municipality.php and municipalitys.php
You need to ensure the methods are present, in your case municipality.php should contain a
public function add()
which is not there. For a reference on how to implement it look at the other controller (most likely you'll invoke the add() method of the relevant model).
Or possibly you're extending the municipality controller from a different ancestor that doesn't implement the add method
Answering my own question, the problem was in the views/municipalitys/tmpl/default.php.
The form contained in that file was wrong, missing the id="adminForm" and the proper action= value.

Joomla form variable handling

I have a situation where I need to create a shortcut for a specific filter in a Joomla component.
The problem is that I cannot unset it, as I do not know if Joomla sets the form fields to its own session handler, request handler or some kind of custom handler. There also does not appear to be any documentation on this specific case.
The full situation is that I have a link that will auto filter in the same view as another link (in the components sidebar). One view will be just a specific filter and the other is standard. So I need it when you click into the filtered view it will reset the current filters to make sure everything displays as it should, and vice-versa so clicking back will again reset the filters.
I have tried a number of approaches for this, and although I can consistently force it to filter but it will not reset the form when I re-enter the last page with any technique I have tried so far and of course I want to avoid bypassing Joomla's default functions.
if(JRequest::getVar('filter_group_id',false)==10){
JRequest::setVar('last_filter',true);
EthicstoolHelper::addSubmenu('supervisors');
}else{
if(JRequest::getVar('last_filter',false)===true){
JRequest::setVar('last_filter',false);
JRequest::setVar('filter_group_id',false)
}
EthicstoolHelper::addSubmenu('users');
}
This is the most recent think I have tried, as you can see I try to reset the value to false in a hope that Joomla will read it as not being set, as JRequest has no built in unset method.
I don't have enough rep to comment yet, so I'm guessing a bit as to what the problem could be. Assuming that you are using a model to set the state of the filters, you can look at overriding the populateState method.
Another options is to fiddle with the context property in the model. For example, you could change the context if you have your special filters enabled if you are using things like $app->getUserStateFromRequest(). If you can post a bit more information about the design of your component (controllers and models), I can help more.

Using Drupal 6's form ahah to have one dropdown field update the values of a second dropdown field

I am working on a pair of form fields the directly effect one another. Specifically the problem I am dealing with is that we have thousands of groups users can join on this website that I have been working on. We are creating some administrative analytics forms for the site. Part of those forms is filtering by group.
The problem is that most of the people don't know the name of a group off the top of their heads, only what it starts with. So instead of using auto-complete we came up with two dropdowns. The first contains the options A through Z, letters of the alphabet which the group names begin with. The second dropdown contains all of the groups beginning with the selected letter.
The concept is simple enough and we were able to implement the AJAX pretty easy with jQuery. But this causes validation errors. After some research I found that this has to do with Drupal form caching. The solution is to use the ahah functions.
The problem with that is that all the instructions I have found so far deal with ahah actions for the specified field updating itself, not another field. So I am trying to figure out how to get the list of letters to update the group list using ahah and registering the new values with the form cache.
One solution I found was to simply load all the values on the page in a hidden dropdown and then just load up those in the set in the visible one as needed. But that creates a bunch of problems because the client uses almost exclusively IE and we have seen IE choke when given huge lists like that. Especially with the lists stored in JavaScript. So we need a light-weight solution.
Any thoughts? Ideas?
In a nutshell AHAH works like this:
Form gets generated with an empty $form_state
When an element that has the #ahah property gets changed an ajax call is made to the url specified as path in the #ahah property.
The callback at that url does this:
Retrieves the form from the form cache
Processes the form
Rebuilds the form with a current $form_state so that it can add / change elements based on that
The form is put in the form cache to avoid validation errors
The form is rendered and returned as JSON
Drupal replaces the contents of the wrapper div with the new form.
In your case you'll need to make your form generation function aware of the values selected in $form_state. It would have to:
Generate the letters drop-down
Look into $form_state and see if there is a letter selected. If so, generate the second drop-down based on the selected value.
The callback function would implement the form caching and rebuilding.
There are good explanations with code for the callback function here:
http://drupal.org/node/348475
http://drupal.org/node/331941
I actually got the answer on another StackExchange site. His suggestion to use the example module to come up with the solution worked very well.
Solution found here: https://drupal.stackexchange.com/questions/35048/using-drupal-6s-form-ahah-to-have-one-dropdown-field-update-the-values-of-a-sec

Categories