event dispatch before rendering 404 page of "file not found" - magento

my intention is simple i just want to get my pages in different formats
e.g
www.my-site.com/product-name.html -- will load the page in html
but i want a json object when i type
www.my-site.com/product-name.json
if their is any event that magneto dispatch before rendering 404 page, that can be very helpful
or i have to rewrite app/code/core/Mage/Cms/controllers/indexcontroller.php
more or less
public function defaultNoRouteAction()
{
$this->getResponse()->setHeader('HTTP/1.1','404 Not Found');
$this->getResponse()->setHeader('Status','404 File not found');
$this->loadLayout();
$this->renderLayout();
}
but i don't know how.

I've done something similar, exporting product data when calling an product-url with .xml instead of .html.
I've added an observer listening on product save event, that adds the product-xml URL into core_url_rewrite.
In that event, you have replace .htm(l) with .xml
e.g.
product-name.htm -> catalog/product/view/id/4 -> load product with ID 4 and render product view template, magento core
product-name.xml -> mycustommodule/product/view/id/4 -> load product with ID 4 and render XML, custom module
So you have to add another module called "mycustommodule" that handles the xml output.
Cheers!

i solution that i found is following,it work perfectly but didn't know for sure is it the MAGNETO-WAY
i rewrite Mage_Cms_IndexController noRoute Method,
public function noRouteAction($coreRoute = null) {
// getting current url
$url = Mage::helper('core/url')->getCurrentUrl();
if ( strstr($url,".jsonp") || strstr($url,".json") || strstr($url,".xml") ) {
// geting file-type like json / xml
$format = substr(strrchr($url, "."),1);
// product-handle
$elem = substr( strrchr($url, "/"),1, strpos( strrchr($url, "/") , ".")-1 );
switch ($format) {
case 'json': case 'JSON': case 'jsonp': case 'JSONP':
$this->getResponse()->setHeader('Content-type', 'application/json'); break;
case 'xml': case 'XML':
$this->getResponse()->setHeader('Content-type', 'application/xml'); break;
}
// loding product byproduct-handle
$rewrite = Mage::getModel('core/url_rewrite')->setStoreId(Mage::app()->getStore()->getId())->loadByRequestPath($elem . ".html");
$pid = $rewrite->getProductId();
$cat = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->loadByAttribute('url_key', $elem );
if (strpos($url,"hs_review")) { echo Mage::getModel("Hs_Json/review")->wrapper($pid,$format);return;}
if ( $pid ) { echo Mage::getModel("Hs_Json/product")->wrapper($pid,$format);return; }
if ( $cat ) { echo Mage::getModel("Hs_Json/category")->wrapper($cat->getId(),$format);return;}
} else {
$this->getResponse()->setHeader('HTTP/1.1','404 Not Found');
$this->getResponse()->setHeader('Status','404 File not found');
$pageId = Mage::getStoreConfig(Mage_Cms_Helper_Page::XML_PATH_NO_ROUTE_PAGE);
if (!Mage::helper('cms/page')->renderPage($this, $pageId)) {
$this->_forward('defaultNoRoute');
}
}
}
}

Related

Laravel 5 - Display different views based on database query

I have the following database design
I have several different Documents I want a user to be able to create. Each document has its own view for the creation of the document, or the editing.
If the document they are trying to create has already been created, they should see the edit page for that document.
At the moment, I have the following
public function create(Project $project)
{
$documentTypesCreated =
$project->document()
->join('document_type', 'documents.id', '=', 'document_type.documentId')
->select('document_type.documentId', 'document_type.name')
->get();
$documentLink = $_GET['documentType'];
if($documentTypesCreated->isEmpty()){
return View::make($documentLink.'Doc.create', compact('project'));
} else {
foreach($documentTypesCreated as $documentName) {
if($documentName->name != $documentLink) {
return View::make($documentLink.'Doc.create', compact('project'));
} else {
$document = Document::find($documentName->documentId);
return View::make($documentLink.'Doc.edit', compact('project', 'document'));
}
}
}
}
Breaking this up, I am doing the following:
Firstly, I get all the Documents that have been created for a Project
$documentTypesCreated =
$project->document()
->join('document_type', 'documents.id', '=', 'document_type.documentId')
->select('document_type.documentId', 'document_type.name')
->get();
I then get the Document the user is trying to create from the URL
$documentLink = $_GET['documentType'];
So lets say that the query returns that this Project has
DocumentA
DocumentB
And the documentLink shows that I am trying to create DocumentB.
Because DocumentB has already been created, I need the edit page for this Document. Firstly, I check if the query returned and Documents
if($documentTypesCreated->isEmpty()){
return View::make($documentLink.'Doc.create', compact('project'));
} else {
}
If it didnt, it will show the create page for the chosen Document. If it did, the else statement will kick in.
Within the else statement, I loop through all the Documents the query returned
foreach($documentTypesCreated as $documentName) {
}
I then do the following, and this is where my logic is failing
if($documentName->name != $documentLink) {
return View::make($documentLink.'Doc.create', compact('project'));
} else {
$document = Document::find($documentName->documentId);
return View::make($documentLink.'Doc.edit', compact('project', 'document'));
}
If the document Name from the query is not equal to the document I am trying to create, then I show the create page for that Document. Otherwise, I show the edit page.
Now lets get back to where I chose DocumentB. I know that I have already created this Document, so I should see its edit page. However, because DocumentA is the first result returned in the query, the above statement looks like the following
if("DocumentA" != "DocumentB") {
return View::make($documentLink.'Doc.create', compact('project'));
} else {
$document = Document::find($documentName->documentId);
return View::make($documentLink.'Doc.edit', compact('project', 'document'));
}
So it is going to show the create page for DocumentB, not the edit page. If DocumentB was the first result returned, then the edit page would be displayed.
I Hope this makes sense. How can I get it displaying the correct view for the document?
Thanks
Controller
Base on your query - you may set these variables manually
$create = true;
$edit = false;
Logic
if ($query == 'any logic') {
$create = true;
$edit = false;
}else{
$create = false;
$edit = edit;
}
Then, send them over to your view.
View
#if($create == true)
//.. Show Create Form
#else
// .. Show Edit Form
#stop
Then, your view will render base the result of your query. I hope this help !

Zend 1 ajax with dojo informatiom person

I'm working with Zend 1 with dojo and do not know how to use ajax . In my particular case that when selecting a select , whether made ​​in consultation ajax back from the database information. To enter a ID from user print the information from user by ajax.In my work I can't use jquery.
Good question!
Not is very productive work with dojo, but is possible make exactly how do you want. You will create p element with information captured in data base
In your form, add attribute 'onChange'
$form->setAttrib('onChange', 'recoveryData()');
In js file do you have a function recoveryData(), something as:
dojo.require("dojo.html");
// clean data
var myNewElement = dojo.byId('myNewElement');
if (myNewElement != null) {
dojo.empty("myNewElement");
}
dojo.xhrPost({
content: {id: dojo.attr(dojo.byId("myElement"), "value")},
url: 'your-base-path/recovery-data/',
load: function (response) {
if (response != false) {
// converte to obj
var obj = dojo.fromJson(response);
// creat new element
var node = dojo.create("span", {
innerHTML: obj.NomeServidor,
id: "myNewElement",
'class': 'row'
}
);
dojo.place(node, dojo.byId("myElement"), "after");
}
}
});
Now, you need create an Action "recoveryDataAction()" in your Controller like this:
$data = $this->getrequest()->getPost();
$id = $data['id'];
if ($this->getrequest()->isXmlHttpRequest()) {
// disable layout
$this->_helper->layout()->disableLayout();
// disable view render
$this->_helper->viewRenderer->setNoRender();
$yourTable = new Project_Model_YourTable();
$row = $yourTable->fetchRow($id);
if ($infos != null) {
echo Zend_Json::encode($row);
return;
}
echo false;
}

Adding step to Magento onepage checkout process

I am try to create an additional step in the Magento onepage checkout process.
I am following the tutorial located at http://www.excellencemagentoblog.com/magento-onestep-checkout-add-step but specifically adding a step at the end before the review.
My folder / file structure is as follows. (Ignore widget.xml)
I have uploaded the code in it's current state to this gist:
https://gist.github.com/Relequestual/5263498
I have the theme set to 'new'.
I am var_dumping the $this->getSteps() which shows that the 'testcheck' returns null.
In config.xml, if I change under gobal, blocks, checkout, rewrite, onepage to the same class with '_TestCheck' on the end, the checkout doesn't display at all, but 'Test Check' appears in the progress section on the right. When I revert this change, it then shows as not being null in the var dump like so...
But, I still don't see the step actually added to the page.
I've not done any magento before, so feel a bit in over my head. I expect there is some problem with the xml configuration files, but I've been working on this for 2 days now, and am somewhat lost as to what else I can try.
I know this question may sound similar to others, which it is, however I can't find a question where the OP has the same symptoms as what I am seeing.
By default magento gives some checkout steps. But Sometime you need to add extra information from the customer for future reference. A common requested customization is to add the Custom Form in default checkout process.
This is not good practice to touch core files. You can do this via overriding Modules.
In this example Comapnyname is Ipragmatech and Module name is Checkoutstep.
Step1: Add Custom step in the checkout process
Open the Ipragmatech > Checkoutstep > Block > Onepage> Checkoutstep.php file and write the following code
class Ipragmatech_Checkoutstep_Block_Onepage_Checkoutstep extends Mage_Checkout_Block_Onepage_Abstract
{
protected function _construct()
{
$this->getCheckout()->setStepData('checkoutstep', array(
'label' => Mage::helper('checkout')->__('Invitation to participation'),
'is_show' => true
));
parent::_construct();
}
}
Step2: Add steps which and where you want in the checkout process
Open the Ipragmatech > Checkoutstep > Block > Onepage> Checkoutstep.php file and write the following code
class Ipragmatech_Checkoutstep_Block_Onepage extends Mage_Checkout_Block_Onepage
{
public function getSteps()
{
$steps = array();
if (!$this->isCustomerLoggedIn()) {
$steps['login'] = $this->getCheckout()->getStepData('login');
}
$stepCodes = array('billing', 'shipping', 'shipping_method', 'payment', 'checkoutstep', 'review');
foreach ($stepCodes as $step) {
$steps[$step] = $this->getCheckout()->getStepData($step);
}
return $steps;
}
}
Step3: Grab the submitted value of custom form and set the values of Custom form
Open the ipragmatech > Checkoutstep > controllers > OnepageController.php and write the following fucntion
public function saveCheckoutstepAction()
{
$this->_expireAjax();
if ($this->getRequest()->isPost()) {
//Grab the submited value
$_entrant_name = $this->getRequest()->getPost('entrant_name',"");
$_entrant_phone = $this->getRequest()->getPost('entrant_phone',"");
$_entrant_email = $this->getRequest()->getPost('entrant_email',"");
$_permanent_address = $this->getRequest() ->getPost('permanent_address',"");
$_address = $this->getRequest()->getPost('local_address',"");
Mage::getSingleton('core/session') ->setIpragmatechCheckoutstep(serialize(array(
'entrant_name' =>$_entrant_name,
'entrant_phone' =>$_entrant_phone,
'entrant_email' =>$_entrant_email,
'permanent_address' =>$_permanent_address,
'address' =>$_address
)));
$result = array();
$redirectUrl = $this->getOnePage()->getQuote()->getPayment() ->getCheckoutRedirectUrl();
if (!$redirectUrl) {
$this->loadLayout('checkout_onepage_review');
$result['goto_section'] = 'review';
$result['update_section'] = array(
'name' => 'review',
'html' => $this->_getReviewHtml()
);
}
if ($redirectUrl) {
$result['redirect'] = $redirectUrl;
}
$this->getResponse()->setBody(Zend_Json::encode($result));
}
}
Step4: Save Custom Form information
When checkout_onepage_controller_success_action
event hook is called. Open the Ipragmatech > Checkoutstep > Model >Observer.php and write the following
class Ipragmatech_Checkoutstep_Model_Observer {
const ORDER_ATTRIBUTE_FHC_ID = 'checkoutstep';
public function hookToOrderSaveEvent() {
if (Mage::helper('checkoutstep')->isEnabled()) {
$order = new Mage_Sales_Model_Order ();
$incrementId = Mage::getSingleton ( 'checkout/session' )->getLastRealOrderId ();
$order->loadByIncrementId ( $incrementId );
// Fetch the data
$_checkoutstep_data = null;
$_checkoutstep_data = Mage::getSingleton ( 'core/session' )->getIpragmatechCheckoutstep ();
$model = Mage::getModel ( 'checkoutstep/customerdata' )->setData ( unserialize ( $_checkoutstep_data ) );
$model->setData ( "order_id",$order["entity_id"] );
try {
$insertId = $model->save ()->getId ();
Mage::log ( "Data successfully inserted. Insert ID: " . $insertId, null, 'mylog.log');
} catch ( Exception $e ) {
Mage::log ( "EXCEPTION " . $e->getMessage (), null, 'mylog.log' );
}
}
}
}
Magento – Add Custom Form in Checkout Extension is a complete solution to add extra step in Checkout process for your ecommerce website. It allow admin to export data from custom table in CSV format.
Visit the link to get this free extension http://www.magentocommerce.com/magento-connect/custom-form-in-checkout.html

Magento Adding Step to Onepage Checkout

I'm attempting to add a very basic (literally just a div with some text and a continue button) step to the onepage checkout but have come up short, in that they don't work (i suspect it's because they were created prior to 1.7.0.2), when following the examples here (Fontis), here (inchoo) and here (Magento Forums).
I've also tried to combined bits from different examples and forum posts but i've gotten no where in the week i've been attempting to do this.
I have been recommended the book 'Php Architect's Guide to E-Commerce Programming with Magento' which i've purchased and will begin reading but i was wondering if someone could kindly point me in the right direction in adding a new step to 1.7.0.2's onepage checkout?
Much appreciated for any help!
I have done this successfully in 1.7.0.2 following this guide:
http://www.excellencemagentoblog.com/magento-onestep-checkout-add-step
In summary, you need to extend/override Mage_Checkout_Block_Onepage::getSteps() to add your new step in the sequence.
Create your new step's block/template (loaded using an Ajax request when the previous step is completed) Your block class will need to extend: Mage_Checkout_Block_Onepage_Abstract
You also need to extend the onepagecheckout javascript as this does much of the heavy lifting. The Prototype library has a nice way of doing this.
Finally you will need to extend the controller class (Mage_Checkout_OnepageController) to override the responses of any existing steps, return the content of your new step (loaded using ajax), and handle the save action of any data (form fields etc) entered in your new step.
By default magento gives some checkout steps. But Sometime you need to add extra information from the customer for future reference. A common requested customization is to add the Custom Form in default checkout process.
This is not good practice to touch core files. You can do this via overriding Modules.
In this example Comapnyname is Ipragmatech and Module name is Checkoutstep.
Step1: Add Custom step in the checkout process
Open the Ipragmatech > Checkoutstep > Block > Onepage> Checkoutstep.php file and write the following code
class Ipragmatech_Checkoutstep_Block_Onepage_Checkoutstep extends Mage_Checkout_Block_Onepage_Abstract
{
protected function _construct()
{
$this->getCheckout()->setStepData('checkoutstep', array(
'label' => Mage::helper('checkout')->__('Invitation to participation'),
'is_show' => true
));
parent::_construct();
}
}
Step2: Add steps which and where you want in the checkout process
Open the Ipragmatech > Checkoutstep > Block > Onepage> Checkoutstep.php file and write the following code
class Ipragmatech_Checkoutstep_Block_Onepage extends Mage_Checkout_Block_Onepage
{
public function getSteps()
{
$steps = array();
if (!$this->isCustomerLoggedIn()) {
$steps['login'] = $this->getCheckout()->getStepData('login');
}
$stepCodes = array('billing', 'shipping', 'shipping_method', 'payment', 'checkoutstep', 'review');
foreach ($stepCodes as $step) {
$steps[$step] = $this->getCheckout()->getStepData($step);
}
return $steps;
}
}
Step3: Grab the submitted value of custom form and set the values of Custom form
Open the ipragmatech > Checkoutstep > controllers > OnepageController.php and write the following fucntion
public function saveCheckoutstepAction()
{
$this->_expireAjax();
if ($this->getRequest()->isPost()) {
//Grab the submited value
$_entrant_name = $this->getRequest()->getPost('entrant_name',"");
$_entrant_phone = $this->getRequest()->getPost('entrant_phone',"");
$_entrant_email = $this->getRequest()->getPost('entrant_email',"");
$_permanent_address = $this->getRequest() ->getPost('permanent_address',"");
$_address = $this->getRequest()->getPost('local_address',"");
Mage::getSingleton('core/session') ->setIpragmatechCheckoutstep(serialize(array(
'entrant_name' =>$_entrant_name,
'entrant_phone' =>$_entrant_phone,
'entrant_email' =>$_entrant_email,
'permanent_address' =>$_permanent_address,
'address' =>$_address
)));
$result = array();
$redirectUrl = $this->getOnePage()->getQuote()->getPayment() ->getCheckoutRedirectUrl();
if (!$redirectUrl) {
$this->loadLayout('checkout_onepage_review');
$result['goto_section'] = 'review';
$result['update_section'] = array(
'name' => 'review',
'html' => $this->_getReviewHtml()
);
}
if ($redirectUrl) {
$result['redirect'] = $redirectUrl;
}
$this->getResponse()->setBody(Zend_Json::encode($result));
}
}
Step4: Save Custom Form information
When checkout_onepage_controller_success_action
event hook is called. Open the Ipragmatech > Checkoutstep > Model >Observer.php and write the following
class Ipragmatech_Checkoutstep_Model_Observer {
const ORDER_ATTRIBUTE_FHC_ID = 'checkoutstep';
public function hookToOrderSaveEvent() {
if (Mage::helper('checkoutstep')->isEnabled()) {
$order = new Mage_Sales_Model_Order ();
$incrementId = Mage::getSingleton ( 'checkout/session' )->getLastRealOrderId ();
$order->loadByIncrementId ( $incrementId );
// Fetch the data
$_checkoutstep_data = null;
$_checkoutstep_data = Mage::getSingleton ( 'core/session' )->getIpragmatechCheckoutstep ();
$model = Mage::getModel ( 'checkoutstep/customerdata' )->setData ( unserialize ( $_checkoutstep_data ) );
$model->setData ( "order_id",$order["entity_id"] );
try {
$insertId = $model->save ()->getId ();
Mage::log ( "Data successfully inserted. Insert ID: " . $insertId, null, 'mylog.log');
} catch ( Exception $e ) {
Mage::log ( "EXCEPTION " . $e->getMessage (), null, 'mylog.log' );
}
}
}
}
Magento – Add Custom Form in Checkout Extension is a complete solution to add extra step in Checkout process for your ecommerce website. It allow admin to export data from custom table in CSV format.
Visit the link to get this free extension http://www.magentocommerce.com/magento-connect/custom-form-in-checkout.html

Magento Custom Router loading controller but nothing else

I'm trying to get some custom routing going on in Magento using the following code (which I've only slightly modified from here https://stackoverflow.com/a/4158571/1069232):
class Company_Modulename_Controller_Router extends Mage_Core_Controller_Varien_Router_Standard {
public function match(Zend_Controller_Request_Http $request){
$path = explode('/', trim($request->getPathInfo(), '/'));
// If path doesn't match your module requirements
if ($path[1] == 'home.html' || (count($path) > 2 && $path[0] != 'portfolios')) {
return false;
}
// Define initial values for controller initialization
$module = $path[0];
$realModule = 'Company_Modulename';
$controller = 'index';
$action = 'index';
$controllerClassName = $this->_validateControllerClassName(
$realModule,
$controller
);
// If controller was not found
if (!$controllerClassName) {
return false;
}
// Instantiate controller class
$controllerInstance = Mage::getControllerInstance(
$controllerClassName,
$request,
$this->getFront()->getResponse()
);
// If action is not found
if (!$controllerInstance->hasAction($action)) {
return false;
}
// Set request data
$request->setModuleName($module);
$request->setControllerName($controller);
$request->setActionName($action);
$request->setControllerModule($realModule);
// Set your custom request parameter
$request->setParam('url_path', $path[1]);
// dispatch action
$request->setDispatched(true);
$controllerInstance->dispatch($action);
// Indicate that our route was dispatched
return true;
}
}
The result is a page where the template has loaded but with no content. If I comment out the $this->loadLayout() / $this->renderLayout() in my controller I can print to screen. But when I try and load a Template and/or Block it breaks somewhere.
home.html also loads fine (as the method returns false if the path is home.html).
Any assistance would be greatly appreciated.
I was implementing something similar to this and came across the same problem(That makes sense, because I copypasted your code)
before $request->setDispatched(true);
I added $request->setRouteName('brands'); (brands is the frontname of my module).
And It worked.Don't know if It'll work for you, but definetely there was something missing so that magento didn't know what layout to apply, because I could tell that teh controller was being reached.

Resources