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
Related
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.
I know there are lot of posts with this problem, but I guess each of them is with different roots of it (at least from what I checked - nothing helped me).
I am trying to fire the event upon click on a button from the user, but I get the upper mentioned exception in a browser alert Mage registry key "_singleton/" already exists.
The part of the config.xml:
.....
<models>
<packagecustomernumber>
<class>Package_CustomerNumber_Model</class>
</packagecustomernumber>
</models>
</global>
<frontend>
<events>
<checkout_type_onepage_save_order>
<observers>
<type>singleton</type>
<class>packageName/customernumber/observer</class>
<method>setCustomerNumber</method>
</observers>
</checkout_type_onepage_save_order>
</events>
</frontend>
And the class itself:
class Package_CustomerNumber_Model_Observer
{
public function setCutomerNumber($observer)
{
die('setCutomerNumber');
}
}
The button which should fire the even it checking out/saving the order, so the event should be correct.
Any suggestions ?
The first thing that pops out is this
<class>packageName/customernumber/observer</class>
That's invalid. This is the node where you're telling Magento what class to use for your observer. As such, the <class/> node should be either the full PHP class name of your observer
<class>Package_CustomerNumber_Model_Observer</class>
Or a class aliases for the model
<class>packagecustomernumber/observer</class>
Also, before running your observer, it helps to make sure you can instantiate your model class. Try running the following code in a Magento loaded environment (script, controller action, phtml template, etc.)
$model = new Package_CustomerNumber_Model_Observer;
var_dump(get_class($model));
$model = Mage::getModel('packagecustomernumber/observer');
var_dump(get_class($model));
If you can't instantiate the class, then Magento won't be able to either (and it's easier to test this first before running through some steps to trigger your observer).
Yes, the "packageName/customernumber/observer" is the source of the problem.
while this class reference is completely incorrect in its structure, the problem actually comes up when your class reference does not match up with your global/models/modulename definition. even when the reference "looks" correct.
The config :
<config>
<global>
<models>
<mymodule>
<class>My_Module_Model</class>
</mymodule>
</models>
<events>
<some_event_tag>
<observers>
<my_event_observer_method>
<class>my_module/observer</class>
<method>myEventObserverMethod</method>
</my_event_observer_method>
</observers>
</some_event_tag>
</events>
</global>
</config>
Will have the same result because "my_module/observer" is not found, since the "my_module" class group node is not configured. The correct use for this sample would have been "mymodule/observer".
So if you run across this error, re-read your config.xml.
Make sure that your config.xml models section contains
<!-- This says that string 'company_module' corresponds to Company_Module_Model pseudo-namespace in getModel() and getSingleton() calls. -->
<company_module>
<class>Company_Module_Model</class>
</company_module>
Otherwise you won't be able to make new model instance.
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);
I have wrote 2 Magento observers and they both do exactly what I want with the exception that they end on the wrong page. In other words, they write the log files, modify the databases, and talk with other servers, but they modify the page to page routing. For example, I have an observer that I used at login that modifies a database, writes a cookie, and writes to a log, but it changes the post log-in page to
http://www.my-web-site.com/index.php/customer/login/post/
and then gives me a 404 error. If I hit "Ctrl" + 'r' then I am logged in at
http://www.my-web-site.com/index.php/customer/account/index/
which is correct. If I change, app/code/local/my_module/my_model/etc/config.xml to
app/code/local/my_module/my_model/etc/config.xml.1 (in other words take out the observer), then Magento routes to the correct page,
I'm thinking that I need router information in config.xml. My current config.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<!-- The root node for Magento module configuration -->
<config>
<!-- The module's node contains basic information about each Magento module -->
<modules>
<!-- This must exactly match the namespace and module's folder
names, with directory separators replaced by underscores -->
<MyCompany_LogIn>
<!-- The version of our module, starting at 0.0.0 -->
<version>0.0.0</version>
</MyCompany_LogIn>
</modules>
<!-- Configure our module's behavior in the global scope -->
<global>
<!-- Defining models -->
<models>
<!-- Unique identifier in the model's node.
By convention, we put the module's name in lowercase. -->
<mycompany_login>
<!-- The path to our models directory,
with directory separators replaced by underscores -->
<class>MyCompany_LogIn_Model</class>
</mycompany_login>
</models>
</global>
<frontend>
<!-- Defining an event observer -->
<events>
<!-- The code of the event we want to observe -->
<customer_login>
<!-- Defining an observer for this event -->
<observers>
<!-- Unique identifier within the catalog_product_save_after node.
By convention, we write the module's name in lowercase. -->
<mycompany_login>
<!-- The model to be instantiated -->
<class>mycompany_login/observer</class>
<!-- The method of the class to be called -->
<method>wrtLogInCookie</method>
<!-- The type of class to instantiate -->
<type>singleton</type>
</mycompany_login>
</observers>
</customer_login>
</events>
</frontend>
</config>
I'm guessing that the login inside Magento uses an observer, and I'm interfering with it.
Besides the , I'm guessing that I could also accomplish a similar thing in the PHP Observer code. My observer is:
<?php
/**
* Our class name should follow the directory structure of
* our Observer.php model, starting from the namespace,
* replacing directory separators with underscores.
* i.e. /www/app/code/local/MyCompany/LogIn/Model/Observer.php
*/
class MyCompany_LogIn_Model_Observer extends Varien_Event_Observer
{
/**
* Magento passes a Varien_Event_Observer object as
* the first parameter of dispatched events.
*/
public function wrtLogInCookie(Varien_Event_Observer $observer)
{
// Retrieve the product being updated from the event observer
$customer = $observer->getEvent()->getCustomer();
$email = $customer->getEmail();
Mage::log('The E-mail is: ' . $email);
$ran_nmbr = rand();
Mage::log('The random number is: ' . $ran_nmbr);
$crnt_dat = date("m-d-Y::H:i:s");
Mage::log('The date is: ' . $crnt_dat);
return $this;
}
}
?>
I have read about routers, but the articles discussed it in terms of landing on some page before the extension is executed. As you can see, I need to land on the right page after the extension is executed.
Inside the PHP observer, I also tried redirects. For example,
Mage::app()->getResponse()->setRedirect(Mage::getUrl('customer/account/login'));
Maybe I need a full URL address or something. I'm sure this is easy to fix, but my ignorance seems to be following me around. Please help if you know something about this.
Unfortunately, it's not quite as simple as an easy fix. You see, if we look at app/code/core/Mage/Customer/controllers/AccountController.php, in the loginPostAction(), we see that the customer/session singleton triggers the customer_login call. However, what is causing the trip-up here is that after that is called, back in the controller, the controller calls $this->_loginPostRedirect(), so all of your rerouting work that you did is overwritten.
How to fix:
After saying it isn't all that simple, I did happen to see a cheat that we can take advantage of:
$session = Mage::getSingleton('customer/session');
$session->setBeforeAuthUrl($forwardToUrl);
You're partially right. The problem you're running into is Magento's redirect mechanism works with a "last one to say something" wins philosophy. If you look at the standard login code in
app/code/core/Mage/Customer/controllers/AccountController.php
You'll see the loginPostAction method ends with a call to
$this->_loginPostRedirect();
which (ultimately) ends up calling some code that looks like this
$this->getResponse()->setRedirect($url);
This may be the code that's causing you a problem, or it may be something else. The general problem is the final call to the response object's setRedirect method will win.
My usual solution to this is setting some sort of global flag (static variable on a class, a flag set with Mage::register) when I want to perform a redirect, and then creating an additional observer for controller_action_postdispatch. In this observer I look for the global flag I set, and if I find it, set the redirect there.
This handled 99% of redirect situations, and should handle yours. The times this won't work are with some admin login cases, as well as URL rewrites.
The admin login contains some redirect code that doesn't use Magento response object
#File: app/code/core/Mage/Admin/Model/Session.php
...
header('Location: ' . $requestUri);
exit;
...
If this redirect is causing you a problem, create a listener for admin_session_user_login_success that uses PHP header redirects before Magento's does.
Similarly, if Magento's using a rewrite object, the following code might run
#File: app/code/core/Mage/Core/Model/Url/Rewrite.php
if ($isPermanent) {
header('HTTP/1.1 301 Moved Permanently');
}
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');
header('Location: ' . $url);
exit;
(that said, the rewrite code will rarely be your problem, as in standard Magento operation is runs before controller dispatch)
How can I set different email templates for Customer Order Confirm email and Admin copy of the same.
I need to add some extra content for the Admin email copy.
Thanks
I am assuming that you are currently using the "copy" feature to send the admin email. Let me know if that's not the case. Because the same email is currently being sent to multiple recipients, it would be difficult to change the content for each recipient. You could send multiple emails with a little bit of code, though, which would allow you to use a different email template for each. This could be achieved by creating a new class:
class MyModule_Model_Sales_Order extends Mage_Sales_Model_Order {
/**
* Sending email with order data
*
* #return Mage_Sales_Model_Order
*/
public function sendNewOrderEmail() {
parent::sendNewOrderEmail();
/**
* Your admin email sending code here. Copy it out of the sendNewOrderEmail
* function in Sales_Order.
*/
return $this;
}
}
And then telling Magento to override the core class inside your module config:
<config>
<global>
<models>
<mymodule>
<class>MyModule_Model</class>
</mymodule>
<sales>
<rewrite>
<order>MyModule_Model_Sales_Order</order>
</rewrite>
</sales>
</models>
</global>
</config>
You'll need to create the template you want and be sure that your overridden model uses that template instead.
Change tag catalog to sales and add namespace with class name, Eg: if namespace is Custom then add Custom_MyModule_Model and Custom_MyModule_Model_Sales_Order, so the resulting XML file would be:
<config>
<global>
<models>
<mymodule>
<class>Custom_MyModule_Model</class>
</mymodule>
<sales>
<rewrite>
<order>Custom_MyModule_Model_Sales_Order</order>
</rewrite>
</sales>
</models>
</global>
</config>
you can check and edit the HTML files in app > locale > en_US > templates > email
Please keep in mind that you are overruling the Sales and not the Catalog.
So to prevent searching why the above code is not working use "sales" instead of "catalog" in the config.xml.
This extension does it: http://codecanyon.net/item/send-new-order-email-to-admin/3198802
Totally worth the $10 given all the time I wasted trying to get the other answer here working. I had just finished making my own custom module for the contact form, but the other solutions here didn't work.