making Url user friendly in magento - php

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! :)

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);

How concatenate variable in url in magento

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));

Remove controller name from url for my custom module in Magento

I have created my own module. In that, I used IndexController. So the URL for me looks like http://192.168.1.25/upload/index.php/capsync/index/api.
I want to shorten the URL like http://192.168.1.25/upload/index.php/capsync/api.
I want to remove controller name. I tried in the config.xml file, but it shows a 404 error. I don't know how to fix this. Any ideas?
Try Magento rewrite functionality..
Open config.xml and add below code. It will make URL http://192.168.1.25/module/index/index/id/5 --> http://192.168.1.25/module/id/5 like this. Change Rule according to your need.
<global>
<rewrite>
<fancy_url>
<from><![CDATA[/module\/(.*)/]]></from>
<to><![CDATA[module/index/index/id/$1/]]></to>
<complete>1</complete>
</fancy_url>
</rewrite>
...
The most common solution for this is to divide your endpoint info different controller-files. In your example, you could change name from IndexController.php to ApiController.php and the method name from ApiAction() to IndexAction().
The code which works for me is as follows.
<global>
<rewrite>
<fancy_url>
<from><![CDATA[/capsync\/(.*)/]]></from>
<to><![CDATA[/capsync/index/$1/]]></to>
<complete>1</complete>
</fancy_url>
</rewrite>

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.

Fullcalendar not rendering inside Magento block

I have a custom block within Magento Community which rewrites the Orders Tab in Adminhtml and replaces it with a Fullcalendar(arshaw.com's one) which displays events for when products are to be shipped.
I have implemented a JSON feed which relies on my custom modules controller to populate and save the data. This works beautifully and I can see the products once I click on Month / Today / Prev / Next. However, this is not how it should work. It should render when the page loads, except all I see are the buttons.
I have tried referencing the tab in javascript which it is in eg.
jQuery('#diagram_tab_orders_content').tabs({
show: function(event, ui) {
jQuery('#dashCalendar').fullCalendar('render');
}
Where #dashCalendar is the id / class of my tag.
Except this does not render the calendar. It does nothing but change the CSS of diagram_tab_orders_content.
I've seen a similar topic on Stackoverflow where you must explicitly reference the tab where the calendar is in order for it to render properly. This is what I'm trying to achieve in order for the calendar to render when the document is ready.
Any suggestions on how to make this calendar render when the document is ready would be much appreciated.
So I've figured out the issue with the calendar and it pertains to the tabs within Magento. Referencing the tabs explicitly within Javascript causes issues with the rendering, thus I had to rewrite the Dashboard block.
Config.xml in my ModuleName/etc folder:
<global>
<!-- Blocks -->
<blocks>
<adminhtml>
<rewrite>
<dashboard>ModuleName_Block_Adminhtml_Calendar</dashboard>
</rewrite>
</adminhtml>
</blocks>
</global>
Then within my ModuleName/Block/Calendar.php file I set the template:
public function __construct()
{ //this creates the template block for the calendar
parent::__construct();
$this->setId('Calendar');
$this->setTemplate('moduleName/calendar/calendar.phtml');
}
Copy/Paste the fullcalendar's JSON Example page provided, set your events: tag to your .php file which generates / requests data from your database using JSON. Customise your Theme if you want using the provided CSS and you will see the calendar render with the events when you log into your Adminhtml. Below you will see 'Populate'. Its an action within my Controller which retrieves the data and echo's out a JSON feed to the Calendar.
events: {
url: "<? echo $this->getUrl('moduleName/adminhtml_calendar/populate') ?>"
},
Now the Calendar renders with events properly.

Categories