Symfony 1.4 wrong URL routing - php

I have some actions defined like "action = '[module]/[action]'" & submit.
The result is '[currentUrl]/[module]/[action]' instead of '[module]/[action]', I can't figure out why, here's the code :
On the front :
<a href="<?php echo ( isset($disabledMajSuivi) && $disabledMajSuivi ? "#" : "javascript:showYesNoDialog('". addslashes(__('enquete.visu.msg.confirmation.majsuivi')). "', 'majSuivi')" ); ?>" >
<?php echo __('enquete.visu.majsuivi');?>
</a>
showYesNoDialog is a javascript function where first arg is the sentence displayed and second arg is callback function, so 'majSuivi' is called back and looks like this :
<script>
function majSuivi(response) {
if(response == 'yes') {
document.forms[0].action = "module_name/majsuivi";
document.forms[0].submit();
}
}
This has been debugged, the condition is true.
The action 'majSuivi' (that is big) ends like this :
$this->redirect('enquete/visu?enq_id='.$enq_id
. (!is_null($personneMorale) ? '&pm_id='.$personneMorale->getPmId() : '') );
But no action is executed because of the wrong url (so actualy this part of code is kind of useless).
So when the URL should be : http://[baseUrl]/index.php/module_name/majsuivi
It is instead : http://[baseUrl]/index.php/enquete/visu/enq_id/24/menu_id/module_name/majsuivi
Where /index.php/enquete/visu/enq_id/24/menu_id/ was the current url just before calling 'majSuivi action.
Each time I click on the "a href" button, it adds the "module_name" to the URL like following :
click -> http://[baseUrl]/index.php/enquete/visu/enq_id/24/menu_id/module_name/majsuivi
click2 -> http://[baseUrl]/index.php/enquete/visu/enq_id/24/menu_id/module_name/module_name/majsuivi
click3 -> http://[baseUrl]/index.php/enquete/visu/enq_id/24/menu_id/module_name/module_name/module_name/majsuivi
etc ...
I don't think it comes from the routing.yml config file, but if someone thinks it could, I'll write the content of it.
Thanks for your help

You should use something like this:
document.forms[0].action = "<?php echo url_for('module_name/majsuivi')?>";
or
document.forms[0].action = "/index.php/module_name/majsuivi";
or if you have this route in your routing.yml
document.forms[0].action = "<?php echo url_for('#your_route_name_from_yaml')?>";
Because 'module_name/majsuivi' is a relative path and will be concatenated with your current url without / at the beginning

Thanks a lot, these are very useful informations, espacially the third option I think.
Actualy I have different servers for testing before deploying. And I've observed two kind of behavior.
Behavior 1 : of every solutions I've tested, only document.forms[0].action = "/module_name/majsuivi"; is working.
Behavior 2 : of every solutions I've tested, none is working.
I've dug into php versions that were different from one testing server to one another, but putting two servers under the same PHP and Apache versions didn't make them have the same behavior about this issue.
As the deployment environment has behavior 1, I solved this issue for now by using the following javascript : document.forms[0].action = "/module_name/majsuivi";.
All of what I explained before was going on my local server, which match with behavior 1.
I've already tested the first option : document.forms[0].action = ""; which doesn't work on any of the servers.
The second one gives me http://[baseUrl]/index.php/enquete/visu/enq_id/24/index.php/module_name/majsuivi which is then not working, and I'm sure that if it doesn't work on local server, it won't on the others.
I have not tested the third solution as I kind of solved my issue for now, but it looks an effective solution, I think it should work. I'll update if I make use of it.
Thanks & Regards

Related

url does not work on server but does on localhost

I got a problem with my page loader, I have a "buttons" function that I have placed in my pagebanner controller (pagebanner.php). It looks like this:
<?php
if(isset($buttons)){
foreach($buttons as $button) {
echo "<a href='$button[1]' class='w3-btn w3-dark-gray w3-margin-
left'>$button[0]</a>";
}
}
?>
When I am making a new file, I require the pagebanner.php So i can make buttons in the new file, Like this:
$buttons = Array(
['Inplannen', '/?page=onderhoudInplannen&kid='.$_GET['kid']
.'&id='.$row['id']],
['Afboeken', '/?page=onderhoudAfboeken&kid='.$_GET['kid']
.'&id='.$row['id']],
);
But the problem is, that it doesn't lead me to that particular page, but I leads my to my default page where I go when the program doesn't recognize the page name. In my url bar it will show me the good url with the good id's but it just doesn't go to the page.
The first link doesn't work, it is on my server:
Link: 192.168.1.9:8000/?page=onderhoudAfboeken&kid=18&id=217
But the second link does work and it is on my localhost:
Link: http://localhost:8000/?page=onderhoudAfboeken&kid=18&id=217
I want the link to work on my server. How can i solve this?
Thanks,
Check if your server isn't a Linux machine, if it is a Linux machine you have to make all your URL's lowercase. Otherwise your Linux server won't find them.

Using PHP's $_SERVER['REQUEST_URI'] on localhost to get query string - alternative?

I'm currently building a simple MVC framework and I've hit a bit of a road block in terms of breaking the URL down on a localhost but also having it work on a live production server as well.
So basically, my localhost URL is:
localhost/project/public/controller/action
The live version would be:
www.example.com/controller/action
My initial thought was to just use $_SERVER['REQUEST_URI'] which will work perfectly on a live server but on my localhost it returns:
/project/public/controller/action
What I need is:
controller/action
I've had a search around and the only answer I could find was to set up a virtual host which I don't really want to do - this code will be shared between people who may or may not know how to set that up so I want to avoid it if possible.
EDIT: For the record - this is the answer I found - How to get the same $_SERVER['REQUEST_URI'] on both localhost and live server
I also can't remove /project/public/ because this folder structure won't always be the same.
So I basically need to get the path up until the public/ part but I can't even use that because the public folder may be called something else.
I know this must be possible because frameworks such as Laravel do it but even looking at the source for that - I can't quite figure it out.
Thanks for any help.
EDIT: Possible Answer
It's odd how often you have a brainwave as soon as you post something...
I've had the thought that I can just run basename(DIR) at my entry point which will give me the folder's name regardless of what it is. I can then use that to remove everything before (in including) the first instance of that folder.
I'll try this out but if there are more elegant solutions out there, I'd still like to hear them.
I also can't remove /project/public/ because this folder structure won't always be the same.
But i assume, you'll always have controller and action parts? If yes, then do this:
$uriParts = explode('/', $_SERVER['REQUEST_URI']);
$count = count($uriParts);
$controller = isset($uriParts[$count - 2]) ? $uriParts[$count - 2] : null;
$action = isset($uriParts[$count - 1]) ? $uriParts[$count - 1] : null;
Try this to get ending string from your REQUEST_URI which isn't part of the server path:
substr($_SERVER['REQUEST_URI'], strlen(dirname($_SERVER['SCRIPT_NAME'])));

jQuery/Drupal Append string to all HREF on site (Any page)

So I have a site with a dozen pages on it using Drupal as a CMS. Wanted to know how to set/append all href links on any page with a dynamic attribute?
so here is an example URL:
http://www.site.com/?q=node/4
and I wanted to append to the URL like so:
http://www.site.com/?q=node/4&attr=1234
Now I have a nav bar on the site and when I hover over the link I see the url but I need to append the &attr=1234 string to the end of it. The string is dynamic so it might change from time to time.
I was thinking jQuery would be a good choice to do this but does Drupal have any functionality as well?
Now I've seen a couple of posts on Stack:
Post 1
Post 2
Problem is I'm learning my way around Drupal and have minimal experience with jQuery but getting better with both. I see the jQuery can replace a HREF but looks like they hard coded the HREF, could jQuery find all HREF's on a page and append the string to it? Also does Drupal have this functionality and what would be the best approach?
Also need this to work for clean or standard URL format, so I think Apache would handle this I just wanted to make sure.
Thanks for any help
EDIT:
Looks like the general consensus is the Drupal should handle this type of request. Just looking for the best implementation. Simple function call would be best but I would like it to dynamically add it to all existing href's as I want this to be dynamic instead of hard coding any url/href calls. So I could add/remove pages on the fly without the need to reconfigure/recode anything.
Thanks for the great tips though
EDIT #2:
Okay maybe I'm asking the wring question. Here is what I need and why it's not working for me yet.
I need to pass a value in the url that changes some of the look and feel of the site. I need it to be passed on just about every href tag on the page but not on User logout or Admin pages.
I see in my template code where the nav links get generated, so I though I could pass my code in the attributes array as the second parm to the function, but that is setting the tag attributes and not the URL attributes.
Now I see the bottom nav links use this Drupal function: menu_navigation_links() in menu.inc but the top nav uses a custom function.
This function in the template.php script looks to be the one creating the links
function lplus($text, $path, $options = array()) {
global $language;
// Merge in defaults.
$options += array(
'attributes' => array(),
'html' => FALSE,
);
// Append active class.
if (($path == $_GET['q'] || ($path == '<front>' && drupal_is_front_page())) &&
(empty($options['language']) || $options['language']->language == $language->language)) {
if (isset($options['attributes']['class'])) {
$options['attributes']['class'] .= ' active';
}
else {
$options['attributes']['class'] = 'active';
}
}
// Remove all HTML and PHP tags from a tooltip. For best performance, we act only
// if a quick strpos() pre-check gave a suspicion (because strip_tags() is expensive).
if (isset($options['attributes']['title']) && strpos($options['attributes']['title'], '<') !== FALSE) {
$options['attributes']['title'] = strip_tags($options['attributes']['title']);
}
return '<a href="'. check_url(url($path, $options)) .'"'. drupal_attributes($options['attributes']) .'><b>'. ($options['html'] ? $text : check_plain($text)) .'</b></a>';
}
not sure how to incorporate what I need into this function.
So on the home page the ?q=node/ is missing and if I append the ampersand it throws an error.
http://www.site.com/&attr=1234 // throws error
But if I mod it to the correct format it works fine
http://www.site.com/?attr=1234
Assuming that when you mean pages, you mean the content type of pages (will work for any other content type as long as it's really content and not something in a block or view).
You can easily replace the contents of any node that is about to be viewed by running a str_replace or with a regular expression. For instance, using str_replace:
function module_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch($op) {
case "view":
$node->body = str_replace($expected_link, $desired_link);
break;
}
}
where you define desired link somewhere else. Not an optimal solution, but non-Javascript browsers (yes, they still exist!) can't get around the forced, desired URLs if you try to change it with Javascript.
I think doing it in Drupal/PHP would be cleaner. Check out Pathauto module: http://drupal.org/node/17345
There is a good discussion on a related topic here:
http://drupal.org/node/249864
This wouldn't use jquery (instead you would overwrite a function using PHP) but you could get the same result. This assumes, however, that you are working with menu links.
I think you should consider exploring PURL ( http://drupal.org/project/purl ) and some of the modules it works with e.g. spaces and context.
I don't suggest you use jQuery to do this. It's a better practice to do this server side in PHP (Drupal).
You can overwrite the links dynamically into your preprocess page function.
On your template.php file:
function *yourtheme*_preprocess_page(&$vars, $hook){
//You can do it for the region that you need
$vars['content'] = preg_replace('/<a href="(.*?)"/i', '<a href="$1&attr=1234"', $vars['content']);
}
Note:
I did not try it, its only a hint.
It will add your parameters to the outside links too.

Passing data from one php page to another is not working - why?

Frustrated php novice here...
I'm trying to pass a "type" value of either billto or shipto from ab.php to abp.php.
ab.php snippet:
<?php
echo '<a href="' . tep_href_link(FILENAME_ABP, 'edit=' . $bill_address['address_book_id'] . '&type=billto', 'SSL') . '">' .
tep_image_button('small_edit.gif', SMALL_IMAGE_BUTTON_EDIT) .
'</a>';
?>
This does add the &type=billto to the end of the url. It looks like this:
www.mydomain.com/abp.php?edit=408&type=billto&id=4a6524d
abp.php snippet:
if ($HTTP_GET_VARS['type'] == 'billto') {
then it does a db update...
The if returns false though (from what I can tell) because the update is not performed.
I've also tried $_GET instead of $HTTP_GET_VARS.
Because the code in abp.php isn't executed until after the user clicks a button, I can't use echos to check the value, but I can see the type in the url, so I'm not sure why it's not executing.
Could really use some direction... whether it's what I need to change, or even just suggestions on how to troubleshoot it further. I'm in the middle of a huge learning curve right now. Thanks!!!
edit:
Sorry, I just realized I left out that after the db update the user goes back to ab.php. So the whole workflow is this:
User goes to ab.php.
User clicks link to go to abp.php.
User changes data on abp.php.
User clicks button on abp.php.
Update to db is executed and user is sent back to ab.php.
Because the code in abp.php isn't executed until after the user clicks a button, I can't use echos to check the valueWhy not?
echo '<pre>Debug: $_GET=', htmlspecialchars(var_export($_GET, true)), "</pre>\n";
echo '<pre>Debug: billto===$_GET[type] = ', 'billto'===$_GET['type'] ? 'true':'false', "</pre>\n";
if ( 'billto'===$_GET['type'] ) {
...
edit: You might also be interested in netbeans and its php module:
"Debug PHP code using Xdebug: You can inspect local variables, set watches, set breakpoints, and evaluate code live. Navigate to declarations, types and files using Go To shortcuts and hypertext links."
try something like this
if ($_GET['type'] == 'billto') {
die("got to here, type must == billto");
this will prove that your if statement is working or not,
it may be that the update part is not working
Before the if statement - try
var_dump($_GET);
And make sure the 'billto' is contained within the $_GET array. Of course, if you have got the debuger setup, you should be able to watch the value of the $_GET array
or try:
var_dump($_REQUEST);
Check the URL of the second page, is it in the correct form? From the code snippet you post, I don't know if there would be a ? after the question mark. Also try to disable the redirect to see if your code is working as it should.
One other thing is you may want to put the new url in a variable first, then put that into the link HTML. It's less efficient, but makes the code easier to read and debug.
Try turning on error reporting, place this at the start of your php script
error_reporting ( E_ALL | E_STRICT)
PHP is very tolerant of typos and accessing subscripts in an array that does not exist; by enforcing strict and reporting all errors you would be able to catch those cases.
If you can't see the output, try this:
function log_error($no,$msg,$file,$line)
{
$errorMessage = "Error no $no: $msg in $file at line number $line";
file_put_contents("errors_paypal.txt",$errorMessage."\n\n",FILE_APPEND);
}
set_error_handler('log_error');
You may have to set some file permissions for the log file to be written to.
Of course, you can get Netbeans and its debugging module too.

javascript-php var post get

That code work:
startSlideshow(<?php echo json_encode(glob("photos-animaux/*.jpg"))?>);
that code dont :
$.post("",{'folder':'animaux'});
startSlideshow(<?php echo json_encode(glob("photos-".$_GET["folder"]."/*.jpg"))?>);
WHY ?, what i am doing wrong ?, help !
why the stupid php fonction just dont make the string right !! ahhhh!
---new infos----
that line work :
startSlideshow(<?php echo json_encode(glob("photos-".$_GET["folder"]."/*.jpg")) ?>);
because if i MANUALLY enter in the address bar ?folder=animaux...bam! work
so the problem shoul be there : $.get("photo-portfolio.php",{folder:"animaux"});
still dont know where !
If you're using $.post() from JQuery, you should use $_POST['folder'] to access your variable. If you use $.get(), then you use $_GET['folder'] in PHP. Try changing that $_GET to $_POST.
Change $_GET["folder"] to $_POST["folder"] ?
You can dump the $_POST to be sure you're getting the right info..
echo '<pre>', print_r( $_POST, 1), '</pre>';
I hope you're not literally writing these two lines together and hope they are interacting, are you?
$.post("",{'folder':'animaux'});
startSlideshow(<?php echo json_encode(glob("photos-".$_GET["folder"]."/*.jpg"))?>);
PHP runs on the server, Javascript in the browser. In the above two lines, if written like this, the PHP is already long done by the time $.post() is called.
PHP processes the code on the server and sends this to the browser:
$.post("",{'folder':'animaux'});
startSlideshow(['something.jpg', 'something2.jpg']);
The browser executes this code:
Post {'folder':animaux'} to "" (no effect whatsoever).
Start a slideshow with ['something.jpg', 'something2.jpg'] (which was already decided by the time the page loaded).
I hope you're aware of this two stage process.

Categories