How concatenate variable in url in magento - php

I'm trying to add Variable in URL in magento. Here is my link:
<?php echo Mage::helper("html")->getUrl("admin/index/test/".$testId); ?>
If i add / at the end of profile then url not concatenate with testId. But if concatenate testId without adding / at the end of profile then it's not concatenate variable id. here is link
<?php echo Mage::helper("html")->getUrl("admin/index/test".$testId); ?>
Can anyone describe me, what i'm missing?

There are a few mistakes:
the first parameter to getUrl must be a route in the form router/controller/action. Additional parameters can be added with the second parameter
the router for the admin URL is called adminhtml. Magento distinguishs between front name (the first part of the URL) and router (the internal value), which makes it possible to have a custom admin URL. This is configured in app/code/core/Mage/Adminhtml/etc/config.xml:
<routers>
<adminhtml>
<use>admin</use>
<args>
<module>Mage_Adminhtml</module>
<frontName>admin</frontName>
</args>
</adminhtml>
</routers>
The last part(s) of a route can be ommitted in the URL if they are "index", so for the URL /admin, the route is adminhtml/index/index. But as soon as you want to add parameters, all parts are required, to distinguish the parameters from controller and action. It looks like you want to add the parameter test=$testId to the existing route adminhtml/index/index, which redirects to the configured start page, by default adminhtml/dashboard/index, or to the login page if you are not logged in.
For admin URLs, you need to use the adminhtml helper (or the adminhtml/url model)
Conclusion
To get the URL admin/index/index/test/$testId, the first parameter must be adminhtml/index/index and the second parameter ['test' => $testId]
echo Mage::helper("adminhtml")->getUrl("adminhtml/index/index", ['test' => $testId]);
Alternative
If you want to build the URL with GET parameters in the form admin?test=$testId, you can use the _query parameter:
echo Mage::helper("adminhtml")->getUrl("adminhtml/index/index",
['_query' => ['test' => $testId]]);

Try this
echo Mage::helper("adminhtml")->getUrl("adminhtml/index/index",array('test'=>$testId));

Related

Magento: adding link in head to every frontend page

I have a module that needs a canonical link injected into <head> on literally every page on frontend. Is there a way to do it? Currently, given my module doesn't need its own page on frontend, nor any controllers whatsoever, I have only set the helper in my config.xml. Now, I do have an xml in layout, but the problem is that I need to change canonical link attributes based on user input(in admin), so XML doesn't fit. Yes, I could indeed fopen said fronted layout xml file, then replace what I need, then write new content back to it, but I wanted to check first whether there's other way for achieving that.
You can hook in on the core_block_abstract_prepare_layout_before event and use the Head block's addLinkRel method to add the link tag.
In your config.xml you need to define an observer like so:
<events>
<core_block_abstract_prepare_layout_before>
<observers>
<your_module>
<class>Your_Module_Model_Observer</class>
<method>addCanonicalLink</method>
</your_module>
</observers>
</core_block_abstract_prepare_layout_before>
</events>
Create an observer class in the Model directory
<?php
class Your_Module_Model_Observer {
public function addCanonicalLink(Varien_Event_Observer $observer) {
$block = $observer->getData('block');
if ($block->getNameInLayout() === 'head') {
// this will add <link rel="canonical" href="http://your-url.com">
$block->addLinkRel('canonical', 'http://your-url.com');
// If you need more attributes on the link tag use addItem instead
// This will add <link rel="canonical" href="http://your-url" attr="Your Attribute">
// $block->addItem('link_rel', 'http://your-url', 'rel="canonical" attr="Your Attribute"')
}
}
}
Update:
Since the core head.phtml template files run echo $this->getChildHtml() (render all children) it is possible to insert tags by adding a core/text block as a child and add a text string to it (just like you already tried with xml). If addItem doesn't fit your needs, this is more flexible as you can insert any string this way. Replace thie $block->addLinkRel line with
$canonical = $block->getLayout()->createBlock('core/text')
->setText('<link rel="canonical" href="http://your-url.com">')
$block->append($canonical);

making Url user friendly in magento

This is my custom module url http://192.168.1.18/upload/index.php/capsync/
I want to call the next action from the controller apiaction with this:
http://192.168.1.18/upload/index.php/capsync/index/api
but remove index in the url:
http://192.168.1.18/upload/index.php/capsync/api
My config.xml page
<rewrite>
<Livelids_Capsync>
<from><![CDATA[#^capsync/index/api/#]]></from>
<to><![CDATA[api]]></to>
<complete>1</complete>
</Livelids_Capsync>
</rewrite>
In Magento, router will parse your URL as follows:
http://yoursite.com/[frontName]/[actionControllerName]/[actionMethod]/
In your case, For URL : http://192.168.1.18/upload/index.php/capsync/index/api
frontName :: capsync
actionControllerName :: indexController
actionMethod :: apiAction
Above is exactly what you want that is next action in the controller which would be 'apiAction'
Similarly, if you want URL : http://192.168.1.18/upload/index.php/capsync/api
Then your actionControllerName will turn to another controller(apiController) & action(indexAction by default) altogether which is what I don't think you want.
Let me know if you don't get this. Thanks! :)

can not find the save action when magento submit a form

I'm new to magento, and I'm confused how magento handle forms.
I'm using sample data given by magento
In my admin panel, I go to catalog,manage product, and try to figure out how magento save those form data to database. let's say I edit the item which id=881, from the url, when render the page,they use app/design/adminhtml/default/default/template/catalog/product/edit.phtml for the template, and in this file, there is lines of code
<form action="<?php echo $this->getSaveUrl() ?>" method="post" id="product_edit_form" enctype="multipart/form-data">
<?php echo $this->getBlockHtml('formkey')?>
<div style="display:none"></div>
</form>
and i tried to find out which action script specified to handle this post form, I echo it to the page and got something like localhost/magento/index.php/admin/catalog_product/save/id/881/key/d092b22cecaae47664a8c9f9eea63a50/, I think admin is the namesapce, catalog_product is the controllers' name, and save should be the action name, and I look for the file located in Mage_Catalog_controllers_ProductController.php and i can not find the save action.
Admin is your module name, which should give away where you should be looking for the controller,
If you open up app\code\core\Mage\Adminhtml\etc\config.xml,
you will notice,
#FIle :82
<admin>
<routers>
<adminhtml>
<use>admin</use>
<args>
<module>Mage_Adminhtml</module>
<frontName>admin</frontName>
</args>
</adminhtml>
</routers>
</admin>
Notice, <frontName>admin</frontName>
thus for above path of the controller is,
app\code\core\Mage\Adminhtml\controllers\Catalog\ProductController.php
will give you the actions related to product in admin, same way you can track your blocks, helpers and models.

How to change URL on a magento extension?

I want to know how I can change the URL of my checkout cart. Whenever I continue to checkout or click on the checkout button, I get to the URL: mypage.com/onepagecheckout
I'm using this extension: http://www.magentocommerce.com/magento-connect/one-page-checkout.html
The only way I got it working was making one rewrite and a redirect, one rewrite from "onepagecheckout" to "checkout" and another one permanent redirect from "onepagecheckout" to "checkout". I don't think this method is best practice, so I would like to know if there's a better option.
I tried going to the app/code/community/IWD/OnepaceCheckout/etc/config.xml and changed frontname to "checkout", but even that did not change anything.
Thanks for your time and help would be greatly appreciated!
Please check in config.xml.here you found that onepagecheckout
just change onepagecheckout according
<frontend>
<routers>
.......
<use>standard</use>
<args>
<module>IWD_OnepaceCheckout/</module>
<frontName>onestepcheckout</frontName>
</args>
.......
</routers>
also changed it onestepcheckout.xml accoding to you wish frontName
Solved the problem by changing the in config.xml on both standard and admin.

Wildcard parameter value and zend navigation

Is it posible to map a page to a specific action + param combination, like this:
<myprofile>
<label>My Profile</label>
<controller>user</controller>
<action>profile</action>
<visible>1</visible>
</myprofile>
<othersprofile>
<label>User Profile</label>
<controller>usuario</controller>
<action>perfil</action>
<visible>0</visible>
<reset_params>0</reset_params>
<params>
<id>*</id>
</params>
</othersprofile>
I'd like that if the uri includes any id param , then the should be active. (I put a * as wildcard but I dont know a correct way to do it)
/user/profile = should set myprofile to active
/user/profile/id/5 = should set othersprofile to active
Any help appreciated.
Thanks
There is a workaround by setting explicitly the page as active in your action.
You could do it like this :
public function perfilAction(){
$this->view->navigation()->findOneBy('controller','usario')->setActive();
// rest of your action code...
Starting from ZF 1.11.8, you should be able to just add the route name in your page params, and it will be active for anything matching this route. (the "route" feature has been added before, but there were some issues with isActive detection, see http://framework.zend.com/issues/browse/ZF-11359)

Categories