Magento - Custom Module Url - magento

I have had a request to change the url of a module.
For example let's say the url is www.example.com/world
I need the url to be www.example.com/hello/world
I know that within the modules config xml you can change the node to whatever you like (provided it's unique) but does not allow prepend /hello/
<frontend>
<routers>
<anexample>
<use>standard</use>
<args>
<module>An_Example</module>
<frontName>hello/world</frontName>
</args>
</anexample>
</routers>
</frontend>
Does anyone have any idea how this can be done ? Some example code or even a point in the right direction would be appreciated!
Sorry if this isn't detailed enough, i'll be happy to post more of my code if required.
Thanks in advance.

You can add rewrites into Magento's URL Rewrite Manager. Haven't tested following code. But hope it helps.
Mage::getModel('core/url_rewrite')
->setIsSystem(0)
->setStoreId($storeId)
->setOptions('RP')
->setIdPath('index.php?cat=c' . $categoryId . '_' . $this->strip($data['name']) . '.html')
->setTargetPath($categoryModel->getUrlPath() . '.html')// Put the actual path
->setRequestPath('index.php?cat=c' . $categoryId . '_' . $this->strip($data['name']) . '.html') // put the path you want to display
->save();

here i can give you idea to reach your solution it will sure help you
<?xml version="1.0"?>
<config>
<global>
<rewrite>
<some_unique_name_to_identify_this_rewrite>
<from><![CDATA[#^/from_this/#]]></from>
<to>/to_this/</to>
</some_unique_name_to_identify_this_rewrite>
</rewrite>
</global>
</config>
And if you want to do url rewrite with Admin functionality you can go throw this link
Also what you want to achieve could be done with Custom Url Rewrite. For example:
request path: hello/world
target path: 'world'
In this way you're making a destination of system rewrite yours start point.
hope this will sure help you

This was pretty simple in the end. Instead of going to the hassle of creating a rewrite for it i just changed my frontname to 'hello' then created a controller called 'worldController'
Then in my indexController i just put a redirect to */world

Please see the following article by Allan Storm about the Magento rewrite
http://alanstorm.com/magento_dispatch_rewrites_intro

Access custom Module controller path as custom HTML Url Link
Say http://domain.com/index.php/customwallpaper/index/create/
will be rewrited to
http://domain.com/index.php/fotomural-personalizado.html
Insert below data in core_url_rewrite table for url rewrite
Mage::getModel('core/url_rewrite')
->setStoreId(1)
->setIdPath('customwallpaper')
->setRequestPath('fotomural-personalizado.html')
->setTargetPath('customwallpaper/index/create') // Put the actual Controller path
->setIsSystem(1)
->save();
-OR-
Run below sql query
INSERT INTO `core_url_rewrite`
(`url_rewrite_id`, `store_id`, `id_path`, `request_path`, `target_path`, `is_system`, `options`, `description`, `category_id`, `product_id`)
VALUES ('7', '1', 'customwallpaper', 'fotomural-personalizado.html', 'customwallpaper/index/create', '1', NULL, NULL, NULL, NULL);

Related

how to know which module a frontname belongs to just looking at the url in Magento 1.9?

For example:
The url is
index.php/catalogsearch/advanced/
how to find out which module it belongs to ? In this case I know that the frontname is "catalogsearch", the controller is "advanced" and the method is "indexAction" ? But to what module does it belong?
In this case it is easy because the name of the frontname and the name of the module match but if they were different? how can we find the module of the frontname ?
The answer is here, in the config.xml file of the module Mage_CatalogSearch :
<routers>
<catalogsearch>
<use>standard</use>
<args>
<module>Mage_CatalogSearch</module>
<frontName>catalogsearch</frontName>
</args>
</catalogsearch>
</routers>
So I hope you have a good IDE which allows you to perform searches on all files. If you're unable to perform a search, you could edit a template used in the page of the URL you're after, and dump this in a log file :
Mage::app()->getRequest()->getControllerModule();
This will give you "Mage_CatalogSearch". Additionnally, you might want to use these methods too :
Mage::app()->getRequest()->getControllerName();
Mage::app()->getRequest()->getActionName();
//Write below code in index.php file or any phtml files.
/**
* get module name
*/
$this->getRequest()->getModuleName();
More details got through the this link.

Magmi url_key slug - how to include sku?

I'm trying to figure out the best course of action to change URL keys in the store to be name-sku.html
Currently MAGMI seems to be dictating the url key, which is fine. Some of our products are not imported through MAGMI, so I think I'd probably have to also use the updateurl event to change the url on save -- I found this here for that -- https://magento.stackexchange.com/questions/24869/manufacturer-in-product-url-key
The question is, how is it best to do this within MAGMI? I'm importing about 300,000 products currently. The URL also needs to change, as it currently just uses the default settings.
I've looked into the wiki here -- http://wiki.magmi.org/index.php?title=Value_Replacer -- I guess the following code could work for the way it is currently working -- but what do I actually type into the value replacer box?
{{ Slugger::slug({item.name}) }}
Does anyone know how to add the sku to this as well? Would I just do something like....
{{ Slugger::slug({item.name} ."-". {item.sku}) }}
Also, does anyone know if this automatically adds the dashes and the .html?
Any guidance would be extremely appreciated.
EDIT:
I have successfully tested this as working in my test environment, but it is not working as expected in my production environment. I'll go over what happens, and what I did.
1) I purged the core_url_rewrite table
2) I deleted all cache
3) I set up MAGMI (updated it to the most current version as well, and triple checked all my settings)
4) MAGMI went through fine, and the url key on about 5 products that I checked for were how I would expect
5) During the catalog url rewrites reindex, all url keys disappeared.
6) After the reindex was done, they were now in the default magento format (using just the name, and not the sku)
Any ideas?
The only difference between production and development that I can think of at the moment that may have anything to do with this is the fact that I did create a module based on the linked manufacturer-in-product-url-key question on production. Now that I think of it, maybe that's the main difference that I need to address..... I didn't think reindexing would have anything to do with that, but maybe I'm wrong.
Anyway, if anyone has any insight, I would still appreciate it. I have a feeling maybe MAGMI should also be rewriting the url_path?? I think I read something about that somewhere.
Ok, I figured this out. I think this is the best approach.
I was mistaken in my question. I'm actually looking to add the mpn, not the sku. Either way, that doesn't change the process.
So, the best thing to do is to use the Value Replacer in MAGMI, and to create a custom module that updates the url on the Magento catalog_product_save_before event.
Here's exactly what I did.....
MAGMI settings (Value Replacer)
Replaced attributes: url_key
New value for url_key: {{ Slugger::slug({item.name}.' '.{item.mpn}) }}
URLKeyChange (Magento Module)
app/code/local/My/URLKeyChange/etc/config.xml
<config>
<global>
<models>
<LeathornURLRewrite>
<class>Leathorn_URLKeyChange_Model</class>
</LeathornURLRewrite>
</models>
<events>
<catalog_product_save_before>
<observers>
<LeathornURLRewrite>
<type>singleton</type>
<class>LeathornURLRewrite/observer</class>
<method>updateurl</method>
</LeathornURLRewrite>
</observers>
</catalog_product_save_before>
</events>
</global>
</config>
app/code/local/My/URLKeyChange/Model/Observer.php
class My_URLKeyChange_Model_Observer {
public function updateurl($observer){
Mage::log('My log entry', null, 'mylogfile.log');
if($observer->getEvent()->getProduct()){
$Product=$observer->getEvent()->getProduct();
$Url='';
if(!is_null($Product->getData('name'))):
$Url=$Url.$Product->getData('name');
endif;
if(!is_null($Product->getData('mpn'))):
$Url=$Url.$Product->getData('mpn').'-';
endif;
//Mage::log('My log entry'.$Url, null, 'mylogfile.log');
$Product->setData('url_key',$Url);
}
}
}
app/etc/modules/My_URLKeyChange.xml
<config>
<modules>
<My_URLKeyChange>
<!-- Whether our module is active: true or false -->
<active>true</active>
<!-- Which code pool to use: core, community or local -->
<codePool>local</codePool>
</My_URLKeyChange>
</modules>
</config>
URL key will always be just dash separated strings. Don't worry, magento will add .html in it dynamically. You can see in magnento backend in any product configuration page, url key is without html.
Also magento will add dash in url key, Replace your "-" by space " ". Otherwise it will take last word of name and SKU as one word. :)

magento how to call custom model

How can I call model in a custom module? Lets assume that model is really simple one and returns just arrays of static data.
I have a directory structure similar to this:
app
local
Mypackage
Module
Models
Model1.php
How can I include Model1.php inside controller? If I go with Mage::getModel('Mypackage/Modul/Model1) it returns error since it searches model inside Mage/Module/Model/Model1.php
Thanks!
Mage::getModel('mypackage_module/modeltest) should work. But first check your config.xml.
You should have declared it like so:
<models>
<mypackage_module>
<class>Mypackage_Module_Model</class>
<resourceModel>modeltest_mysql4</resourceModel>
</mypackage_module>
<modeltest_mysql4>
<class>Mypackage_Module_Model_Mysql4</class>
<entities>
<modeltest>
<table>mypackage_module</table>
</modeltest>
</entities>
</modeltest_mysql4>
</models>
Mage::getModel('modulename/modelname')->modelmethod();
i.e.
Mage::getModel('catalog/product')->getName();
Hope this help you
This should work:
Mage::getModel('module/model_model1')->modelmethod();
Please note that the model folder should be "Model" not "Models"
Hello you can try this but I have not tested it
$collection = Mage::getModel('Module/Model1')->getCollection();

Magento Router Url - Need Hyphnated Path Name

Let's say I use a custom controller to have a url path/frontend name of
/customcategory
Well, obviously if I have a controller file named 'TestController.php' and indexAction
the url path would be
/customcategory/test/index
What I am trying to figure out is how I do re-name the Test Controller, or modify the config xml file, so I can have a hyphenated url from a controller file such as
/customcategory/test-section/index
I know that if I want /customcategory to be hyphenated, I can just modify the frontend tag in the config file. But the site I am building would benefit from a hyphenated controller route, the part that comes after /customcategory with keywords and I cannot get it to work nor can I find an example on google - as crazy as that may seem.
Thanks for your time.
What you are trying to do is possible using global rewrite in your custom module. You could pass all incoming request for /customcategory/* to a specific controller action. But you would have to manage your own route (base on the depth of your url path).
e.g www.MageIgniter.com/customcategory/path1/path2
config.xml
<global>
<rewrite>
<fancy_url>
<from><![CDATA[/customcategory\/(.*)/]]></from>
<to><![CDATA[customcategory/index/processroute/tagname/$1/]]></to>
<complete>1</complete>
</fancy_url>
<rewrite>
</global>
<frontend>
<routers>
<tagseo>
<use>standard</use>
<args>
<frontName>customcategory</frontName>
</args>
</tagseo>
</routers>
class MageIgniter_Customcategory_IndexController extends Mage_Core_Controller_Front_Action
{
public function processRoute(){
print_r($requestUri = Mage::app()->getRequest()->getRequestUri()); //path1/path2
print_r($this->getRequest()->getParam('tagname')); // path1
print_r($this->getRequest())
// do you custom logic here base on about request path explode('/', trim($requestUri,'/'))
}
...
For a working example see "Product Tags" section # http://www.contempospace.com/bedroom-furniture/wardrobe-closets/custom-closet-systems/isa-closet-system-shelves-hanging-walk-in-reach-in-closet.html
As far as I am aware you cannot add hypens in the url to match up to a filename. If you are trying to get a folder structure you can just add more paths to it.
For example if you wanted:
Namespace/CustomCategory/controller/test/SectionController.php
you could do:
/customcategory/test_section/index

Magento: rewriting multiple URLs (from) to one target (to) using a regex?

I want to rewrite some of my URLs using a regex. This is my rewrite stuff in config.xml of my module.
<rewrite>
<My_Module>
<from><![CDATA[#^/(abc)|(def)/configuration/#]]></from>
<to>/config/configuration/</to>
<complete>1</complete>
</My_Module>
</rewrite>
As you see, what I am trying to achive is to rewrite all urls that contain abc/configuration or def/configuration. This does not work. So how can I add multiple URLs to the same rewrite in Magento? I dont care if its done with a regex or a line for each rewrite in config.xml, so far I did not manage to figure it out either way.
Thanks!
Addition: I solved this now by using:
<from><![CDATA[#^/.+/configuration/#]]></from>
This did the trick in my case :) ... I am still interested how to solve it the first way though. If anybody knows, please post anyway!
Select first OR second is written like this /(first|second)/
Code bellow is working in 1.8 magento.
$2 will be replaced with what ever (.*) captures.
<config>
<global>
<rewrite>
<My_Module>
<from><![CDATA[#^/(abc|def)/configuration/(.*)#]]></from>
<to><![CDATA[/config/configuration/$2]]></to>
<complete>1</complete>
</My_Module>
</rewrite>
</global>
<config>

Resources