Cake php create theme
Ever wondered how to create an application that supports
themes in cakephp?
Here is the example how this can be done.
step1:
In the AppController add the following function.
function beforeRender()
{
$this->viewClass = "Theme";
$this->theme = "two";
}
if the function is already there then add the following lines in it
$this->viewClass = "Theme"; $this->theme = "two";
Description:
here we are telling the controller to load the class ThemeView to render all the templates, this class is located in the
lib/Cake/View
folder having a file name “ThemeView.php”
(note that you can have your own class Also, provided it extends the class “View”
the member variable theme is set to two here, which will be the folder to look for the template(.ctp) files.
Step2:
Now copy all the files from the
[CAKE_ROOT]/app/View/ folder to the [CAKE_ROOT]/app/View/Themed/two/
folder
if the Current Controller is Pages and the current action is index then the ThemeView class will look for the file
Pages/index.ctp
in the
[CAKE_ROOT]/app/View/Themed/two/
folder having path
[CAKE_ROOT]/app/View/Themed/two/Pages/index.ctp
you can add your theme specific css in the default layout file.
To change themes just change the $this->theme variable and create directory structure accordingly.
It is not necessary to copy all the files from the [CAKE_ROOT]/app/View/ folder to the [CAKE_ROOT]/app/View/Themed/two/folder if any file is missing in this folder CakePhp will look for the file in its default location.
codecode
redirect all missing actions to home page
to redirect all missing actions to home page – i assume here that for home page the controller is index and the action is index, add the following lines to your controller(s)
if you want to change it universally then add in the AppConctoller.php (for cakephp ver 2.1.0) in function afterFilter()
if ($this->response->statusCode() == '404')
{
$this->Session->setFlash(
'The Information that you want to request cannot be found');
$this->redirect (
array (
'action' => 'index',
'controller' => 'index'
),404,true );
}
dumping all existing variables – php
to dump all the defined variables use the following code
var_dump(get_defined_vars());
multi store in magento
To host more than one store on a magento installation use the following code in your index.php file by replacing the line
Mage::run();
switch($_SERVER['HTTP_HOST']) {
// storeone.com
case 'storeone.com':
case 'www.storeone.com':
Mage::run('storeone', 'store'); // where storeone is the store code for www.storeone.com
break;
// storetwo.com
case 'storetwo.com':
case 'www.storetwo.com':
Mage::run('storetwo', 'store'); // where storetwo is the store code for www.storetwo.com
break;
// (default store)
default:
Mage::run();
break;
}
remove extra line and spaces from text – php
The removal of extra line, tab , spaces can be done by using a simple user defined function which is
function getStripedDescription($description)
{
$description=str_replace("\r\n",'',$description);
$description=str_replace("\n\r",'',$description);
$description=str_replace("\n",' ',$description);
$description=str_replace("\t",' ',$description);
$description = preg_replace("/ {2,}/", " ", $description);
$description=trim($description);
return $description;
}
to get the cleaned description just call the function
getStripedDescription($text);
where $test is the variable that you want to clean.
I know that the function could be improved by the use of regular expression and could be shortened to one line only but for the ease of understanding i am using the current form.
get store url – magento
To get the url of a store provided you know the store id use the following code
$store=Mage::getModel('core/store')->load($storeId);
return $store->getUrl();
associated product of a configurable product – magento
to get all associated product of a configurable product let’s say the one with the entity id 555
you could use the following code
step 1:
first create the object of the catalog/product model
by the statement
$productModel=Mage::getModel('catalog/product');
step 2:
Now that we have the object load the product with the id 555
use the statement
$productObj=$productModel->load(555);
step 3:
The variable $productObj is now loaded with all the attribute of
the product whose entity id is 555 and all the attribute can be
fetched by
echo $productObj->getData('');
where
the simplest method will be to use the function
getUsedProducts()
on the product attribute to get the child product but this not always return the associated Products
in order to get the associated product first get the type instance of the
product ie catalog/product_type_configurable to get it use the statement
$productObj->getTypeInstance();
and to get the associated product use the statement
$associatedProduct=$productObj->getTypeInstance()
->getUsedProducts();
Other Methods
$product = Mage::getModel('catalog/product')->load(555);
$AssociatedProduct=Mage::getModel('catalog/product_type_configurable')
->getUsedProducts(null,$product);
change parent iframe location – javascript
there are many cases in which you would like to change the location of the parent window from iframe to do the task write the following code in the iframe window
<script type="text/javascript"> window.parent.location='http://www.defyi.com'; </script>
the above code will change the location of the parent document to http://www.defyi.com when executed from an iframe
make sql query the magento way – magento
suppose you want to make sql query in magento that list all the categories
you can use my post or if you want to follow the magento method you can use the following
step 1. make an object of catalog model.
$obj=Mage::getModel('catalog/category');
the above code gives us the object of Mage_Catalog_Model_Category class .
step 2. Now that we have the object of the Mage_Catalog_Model_Category class lets now make the object of Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat_Collection by using
$obj=Mage::getModel('catalog/category')->getCollection();
try
$obj->printlogquery(true);
the above will print the sql query
SELECT `main_table`.`entity_id`, `main_table`.`level`, main_table`.`path`, `main_table`.`position`, `main_table`.`is_active`, `main_table`.`is_anchor` FROM `catalog_category_flat_store_1` AS `main_table
which list the six attribute viz.
entity_id level path position is_active is_anchor
step 3. now if you want to select the name of the category also then you will have to do
$obj=Mage::getModel('catalog/category')->getCollection();
$obj->addAttributeToSelect('name');
gives you sql as
SELECT `main_table`.`entity_id`, `main_table`.`level`, `main_table`.`path`, `main_table`.`position`, `main_table`.`is_active`, `main_table`.`is_anchor`, `main_table`.`name` FROM `catalog_category_flat_store_1` AS `main_table`
if you want only the active categories then use
$obj=Mage::getModel('catalog/category')->getCollection();
$obj->addAttributeToSelect('name');
$obj->addAttributeToFilter('is_active',1);
gives you sql
SELECT `main_table`.`entity_id`, `main_table`.`level`, `main_table`.`path`, `main_table`.`position`, `main_table`.`is_active`, `main_table`.`is_anchor`, `main_table`.`name` FROM `catalog_category_flat_store_1` AS `main_table` WHERE (is_active = '1')
to print the list of categories thus returned
foreach ($obj as $o)
{
print $o->getData('name');
}
note
you can always use
$obj=Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addAttributeToFilter('is_active',1);
to get the above result
prevent link from being followed – jquery
look at the link
<a href="http://www.defyi.com" id="link1">follow me</a>
There may be a situation that you want to not follow the link and change the link title
This can be done by the jquery following code
$(document).ready(
function(){
$('#link1').click(
function(event)
{
event.stopPropagation();
$('#link1').text('clicked so what next');
return false;
});
});
Recent Comments