joomla mvc component redirect with friendly url - php

I got a code like this in my controller
class MyController extends MyBaseController {
function redirectToCart() {
$link = JRoute::_('index.php?option=com_foo&view=cart');
$this->setRedirect($link);
}
}
I also created a menu associated with my view cart in menu name "View Cart" every time i click this button the url is domainname.com/view-cart but when use redirect in mvc the url is domainname.com/index.php?option=com_foo&view=cart
How can i create a redirect in mvc that it work with the front-end link or at least create a user friendly url

You need to pass Itemid in url to create desired seo:
$link = JRoute::_('index.php?option=com_foo&view=cart&Itemid=your_itemid');
You should turn of SEO and look what Itemid is in your menu item a use it in your url.
Or you can do it dynamically like this:
$itemid = JRequest::getint( 'Itemid' );
Then the $link would look:
$link = JRoute::_('index.php?option=com_foo&view=cart&Itemid='.$itemid);
Or you can take it from whatewer menu item you want like this:
$item = JFactory::getApplication()->getMenu()->getItem( $menuitem );//$menuitem is the id of menu
$itemid = $item->id;

Related

how can i return to page with same search filters

In cakephp I can add search filters and change the post vars into Get as below. This works fine.
My problem is how do go to another page found from the list of searches and return to the same page? All search filters are gone when i return as I am not storing them.
eg I search for invoice 2 and get a list of searches. i click on link from the search and view a page. I then want to return to the same page with same page number for invoice 2?
if (($this->request->is('post') ||$this->request->is('put'))&& isset($this->request->data['filter'])) {
$filter_url['controller'] = $this->request->params['controller'];
$filter_url['action'] = $this->request->params['action'];
$filter_url['page'] = 1;
// for each filter we will add a GET parameter for the generated url
foreach($this->data['Filter'] as $name => $value){
if($value){
$filter_url[$name] = urlencode($value);
}
}
//Post params are now GET paramaters
return $this->redirect($filter_url);
}//isset
Options one: use link/button with javascript window.history.back()
Options two: use Controller::referer() in your view like
echo $this->Html->link('Back', Controller::referer());
but this is only allow you to one step back.

Magento seo friendly url in custom module

I would like to create porfolio module, but I need it to be seo friendly, so url should look like:
example.com/portfolio/projectid
where portfolio - module, so it will be index action
projectid - based on field alias
its easy to create example.com/portfolio but how to create controller, or detect what is after?
The same URL hierarchy is in products, so it's category/product
Anybody have idea how to do it?
Below code works for item/id. you can change with your requirement.
$model = Mage::getModel('items/items');
/*Rewrite */
$isSystem = 0; // set 0 for custom url as we have created custom for profile extension
$_itemId = $model->getModuleItemId();//module_item_id field
$_itemName = $model->getTitle();//title field
$_itemName = strtolower(str_replace(" ", "", $_itemName));
// save profile view url rewrite
$viewIdPath = 'item/id'.'/'.$_itemId;
$viewRequestPath = 'items/'.$_itemName;
$viewTargetPath = 'items/index/item/id/'.$_itemId;//controller is itemsController.php, Action is itemAction()
$_coreUrlRewrite = Mage::getModel('core/url_rewrite');
$_coreUrlRewrite->load($viewIdPath, 'id_path'); // check if item path already saved? If yes, $_coreUrlRewrite will contain existing data.
$_coreUrlRewrite->setStoreId($_storeId)
->setIdPath($viewIdPath)
->setRequestPath($viewRequestPath)
->setTargetPath($viewTargetPath)
->setIsSystem($isSystem)
->save();
if(isset($viewRequestPath)){
$model->setUrl($viewRequestPath);
$model->save();
}
/*Rewrite End*/
You can find more info here & here

Joomla get content by ajax

How to get joomla content using ajax? (I want to show content of specyfic page in popup), this is my code: (called by ajax)
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
if ($option=="com_content" && $view=="article") {
$ids = explode(':',JRequest::getString('id'));
$article_id = $ids[0];
$article =& JTable::getInstance("content");
$article->load($article_id);
echo '<h2>'.$article->get("title").'</h2>';
echo $article->get("introtext"); // and/or fulltext
}
This works fine only for artilces, but the problem is when for example I want to show category, or component
Please see your if condition it checks if option is equal to com_content & view is equal to article only. If view contains category it won't work. So add the conditions in if statement so that your code gets executed.
for category you need to add view=category & like for other components as well.

how to ajaxify zend_pagination results (already working with partialoop) using jquery

in the controller i have :
$paginator = Zend_Paginator::factory($mdlPost->getPosts($this->moduleData->accordion, 'name ASC'));
if(isset($params['cities'])) {
$paginator->setCurrentPageNumber(intval($params['cities']));
}
$paginator->setItemCountPerPage(4);
$this->view->posts = $paginator;
in the view's i have some thing like this :
if ($this->posts != null) {?>
<div id="cities_accord" class="news">
<?php echo $this->partialLoop('partials/post-min.phtml', $this->posts); ?>
</div>
<?php echo $this->paginationControl($this->posts,
'Sliding',
'public/pagination_cont.phtml');
}
the partial/post-min.phtml
<?php
$color = array(1=>'spring',2=>'summer',3=>'autumn',4=>'winter');
?>
<div id='<?php echo $color[$this->partialCounter] ?>' class="accordion_post">
<?php
$link = Digitalus_Uri::get(false, false, array('openCity' =>
$this->id));//$color[$this->partialCounter]));
?>
<h1 class="accordion_post_title"><?php echo $this->title ?></h1>
<p><?php echo $this->teaser ?> <i>read more</i></p>
</div>
the pagination_cont.phtml taken from this link zend ( http://framework.zend.com/manual/en/zend.paginator.usage.html )
will show links that will pass params to the controller to fetch the corresponding whole page which is working alright for now
but i want to change this so that i will be able ajaxify the returned ( i.e. only a single paginated value rather than reloading the whole page ) results how can i do that using jquery and what should i change ..
** EDIT: it would be nice to have a fail-save ,if possible, for browsers(users) that disabled javascript to see the same thing by reloading the page (i.e. keeping the current status for if(javascript_not_enabled ))**
This is what I've done in the past.
First, setup the AjaxContext action helper to enable the html context on your controller action.
Add a .ajax.phtml view that just contains the section of markup that may be replaced via AJAX as well as the pagination control links. You can probably just copy this out of your normal view. Replace that section in your normal view with something like
<div id="reloadable-content">
<?php echo $this->render('controller/action.ajax.phtml') ?>
</div>
This will ensure that your initial and any non-AJAX requests will still include the right content. The <div> id is purely for referencing the loadable block in JavaScript.
Also make sure you include your JS file (using headScript) in the normal view only.
Now, in your JS file, unobtrusively add the appropriate event binding to the paginator links. As you'll be replacing the pagination control section in order to reflect the correct current page and other links, it's probably best to do this using the jQuery live binding. I'm also assuming you'll wrap the pagination control with some kind of identifiable element (<div class="pagination-control"> for example)
$('.pagination-control').find('a').live('click', function(e) {
var link = $(this);
$('#reloadable-content').load(link.attr('href'), { format: 'html' });
return false;
});
Keep in mind that in using this method, you will lose the ability to navigate the paged requests using the normal back / forward browser buttons. You will also lose the ability to bookmark pages directly (though you could always provide a permanent link to the current page as part of the AJAX loaded content).
You can use something like the jQuery history plugin if you're really concerned but that will require more client-side work.
Another caveat is that the above will only work with pagination links. If you want to use a form with dropdown page selection, you need to add another event handler for the submission.
GOT IT and big Thanks to #Phil Brown :
in the controller int() change the response type to json
class NewsController extends Zend_Controller_Action
{
public function init()
{
$contextSwitch = $this->_helper->getHelper('contextSwitch');
$contextSwitch->addActionContext('list', 'JSON')
->initContext();
}
// ...
}
public listAtcion() {
// .............
$paginator = Zend_Paginator::factory($mdlPost->getPosts($this->moduleData->accordion, 'name ASC'));
if(isset($params['cities'])) {
$paginator->setCurrentPageNumber(intval($params['cities']));
}
$paginator->setItemCountPerPage(4);
$post = array();
foreach($paginator as $post ) {
$post[] = $post;
}
$this->view->post = $paginator;
#TODO //add a check here for non-ajax requests (#improvment)
$this->view->posts = $paginator;
}
in one of the views (most probably in the pagination_cont.phtml) on the pagination controller add the ajax links
<?= $this->ajaxLink (
$this->url('cities'=>$this->page_num),array('id'=>'div_id','complete'=>'js_method(json_data)','method'=>post) ,array('format'=>'JSON'));
and add a JavaScript function of js_method(json_data) to modify the div with id = 'div_id' with a json data
function js_method(json_data) {
var content = parse.JSON(json_data);
$('#div_id').html('');
//fill it with the reposnse content some thing like $('#div_id').append(content);
}

Joomla : how to get the url of a specific Menu itemID?

Friends a newbie question.........I need help in getting the URL of a specific Menu itemID. The situation is like this:
I am running Joomla and asking for a user to input for a menu ID and choose a layout for that menu ID.
I want to do something else with this URL of the Menu itemID.
How can I get the URL of this Menu itemID provided by the user?
For Example if the user input is liek $this->get ('menulayoutid'>; and he inputs and ID of 54 then how do I get the URL for Menu ID 54.
Please note: I want to get this URL from within my PHP file and not in the browser so that I can use the value of that URL for some other purpose.
Kindly help.
$itemid = JRequest::getVar('Itemid');
$application = JFactory::getApplication();
$menu = $application->getMenu();
$item = $menu->getItem($itemid);
$link = new JURI($item->link);
$link->setVar('ItemId', $itemid);
Source: http://forum.joomla.org/viewtopic.php?p=1836005
However, we get the Itemid from anywhere (user input, from our own developed module using the "menu item" field type in the xml file as described in the Joomla Docs - Standard form field types)
// get the menuItemId from wherever...
// as described above or as in other posts here and do whatever with that!
$menuItemId = 'fromWherever'; // as an example "107";
// build the link to the menuItemId is just easy and simple
$url = JRoute::_('index.php?Itemid=' . $menuItemId);
i think if we need only a link to a specific menu id, this is the best solution, because we have absolutely less requests and a clean code
this works also in Joomla 3.0, 3.1
I just want to add that if you need to target a specific menu you pass the menu name as an argument to getMenu().
$itemid = JRequest::getVar('Itemid');
$application = JFactory::getApplication();
$menu = $application->getMenu( 'menu-name' );
$item = $menu->getItem($itemid);
$link = new JURI($item->link);
$link->setVar('ItemId', $itemid);
I'm not sure if Joomla changed the way this works since 2.5 or even 1.7 but I spent the worse half of 2 hours looking for this.
Hopefully it helps someone.
$menuID = $params->get('menuItem'); // from module field menu ex. '105'
$js = new JSite;
$menu = $js->getMenu();
$link = $menu->getItem($menuID)->route;
//Returns URL Friendly Link -> menu/article
//Then format it ->
$link = 'http://www.yoursite.com/index.php/'.$link;
echo 'Borrowed Menu Link Path";
When you need to get your active menu item ID in Joomla to display some specific content for only that menu item or just to show the ID of the menu item, insert the following code where you wish to display the active menu item ID:
<?php
$currentMenuId = JSite::getMenu()->getActive()->id;
echo $currentMenuId;
?>

Categories