how to make codeigniter routing to work with paging - codeigniter

I have the following routes for the news that works like a charm:
$word5 = urlencode('mygreek-keyword-here');
$route[$word5] = "news/displayAllNews";
and this page display all news with the desired keyword in the address bar. But...there is always a but...I have paging on this page, so when i click on next page link...page works but with old non-friendly seo url, that is:
news/displayAllNews/10/10
while I would like to have is: mygreek-keyword-here/10/10
code that i have in my controller for the paging is as follow:
$config['base_url'] = site_url('/news/displayAllNews/'.$limit.'/');
what should i do in routes.php and in my controller to get the desired result?
Regards, John

You could get the string of URI by $this->uri->uri_string() method, and set the pagination config as follows:
$config['base_url'] = site_url($this->uri->uri_string() .'/'. $limit.'/');
Note that the URI library is loaded automatically, it doesn't need to load it manually.
Update:
Change the route rule to:
$route["$word5(.*)"] = "news/displayAllNews$1";
If it doesn't work yet, remove the .'/'. $limit.'/' phrase and let the pagination to add the limit itself by:
$config['per_page'] = 10; // or $limit, default items per page.
$config['use_page_numbers'] = FALSE;

Related

Routing issues with Codeigniter project

In my routes.php file I have the following codes:
$route['admin/login'] = 'admin/login/index';
$route['admin/add_client'] = 'admin/add_client/index';
$route['(:any)'] = function ($val){
require_once( BASEPATH .'database/DB.php' );
$db =& DB();
$db->select('url');
$db->from('interior_form');
$db->where('url',$val);
$query = $db->get()->row();
$db->close();
if(sizeof($query)>0):
return 'home';
else:
return "404_override";
endif;
The issue that I am facing here is whenever I put www.xyz.com/admin/login , it goes to the home page first then again if I write www.xyz.com/admin/login in the same browser, only then it goes to the admin login page.It does not go to the admin login page on the very first instance.
Try:
$route['admin/login'] = 'admin/login/index';
$route['admin/add_client'] = 'admin/add_client/index';
$route['(:any)'] = 'home/$1';
In your home controller, there you handle which request is 404 and which is valid.
You don't need to edit your Rout file.first of all come out from that file.
and you can do this.....
In admin controller page create a function called index.in that function you load your login page.
whenever you type www.xyz.com/admin then load the admin login page.
note:-
Which controller page contains index function,first that index function is load when call that controller, because of index function is first priority compare to other functions in all controllers page.
You may try this simple code for set up rout as bellow.
$route['default_controller'] = 'Adminlogin';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

Codeigniter admin controller in subfolder issue while pagination

I am new to codeigniter.i have create subfolder for admin controller like
Controller->admin->news.php
Now when i am access the news controller its working fine like
http://mysite.com/sacha/adminenter code here/news
But when i am trying edit delete or pagination like
http://mysite.com/sacha/admin/news/index/1
or
.../sacha/admin/news/1
Its showing 404 page not found error
Routes which i am using is
$route['admin/news'] = 'admin/news';
$route['admin/news/index'] = 'admin/news/index';
$route['admin/news/(:num)'] = 'admin/news/$1';
$route['admin/news/index/(:num)'] = 'admin/news/index/$1';
I used (:any) also but none is working.
Thanks
remove your four line you specified above and just update your routes as this,.
$route['admin/news/index/(:num)'] = 'admin/news/index/$1';
$config['uri_segment'] = 4;
You have to include this config parameter in pagination.

Pagination on Pyrocms pages

I am currently displaying a list of products on pyrocms page. Using plugin i get the products from db tables and display them as a list i want to use pagination but have no idea how to use it and where to start from. Does any one know any tutorial or have some suggstion?
You won't need to load pagination library or initialize it. It is a little different from you do in regular codeigniter, also you can surely use the way you do it in codeigniter pagination class
I usually do this for pagination.
in your controller use this
....
// Create pagination links
$total_rows = $this->products_m->count_all_products();
$pagination = create_pagination('products/list', $total_rows);
//notice that product/list is your controller/method you want to see pagination there
// Using this data, get the relevant results
$params = array(
'limit' => $pagination['limit']
);
$this_page_products = $this->product_m->get_all_products($params);
....
//you got to pass $pagination to the view
$this->template
->set('pagination', $pagination)
->set('products', $this_page_products)
->build('products/view');
obviously, you will have a model named products_m
At your model this would be your get_all_products function, I am sure you can build the count_all_products() function too
private function get_all_products($params){
//your query to get products here
// use $this->db->limit();
// Limit the results based on 1 number or 2 (2nd is offset)
if (isset($params['limit']) && is_array($params['limit']))
$this->db->limit($params['limit'][0], $params['limit'][1]);
elseif (isset($params['limit']))
$this->db->limit($params['limit']);
//rest of your query and return
}
Now at your view you have to foreach in your passed $products variable and to show pagination links use this line in your view
<?php echo $pagination['links'];?>
I use this in my works, hope it help.
Pyrocms used codeigniter framework this code works perfectly in case of module but i have never tried this on a plugin but still you can try this.
Load pagination library in Controller
$this->load->library('pagination');
$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
Use this code in view to genrate the links
echo $this->pagination->create_links();

How to route everything to default controller in codeigniter?

I have a website http://example.com built using codeigniter 2. the default controller is
$route['default_controller'] = "domain";
If I try to access pageX, the link should be http://example.com/en/domain/view/pageX.
I want to allow the visitor of the website to access this page by typing
http://example.com/pageX
I have tried
$route['(:any)'] = "view/$1"; ==> it gives 404 Page Not Found
$route['(:any)'] = "domain/view/$1"; ==> it redirects to homepage with link shown as http://example.com/en/pageX
$route['(:any)'] = "en/domain/view/$1"; ==> it gives 404 Page Not Found
but non of them worked for me.
EDIT
by adding this:
$route['(:any)'] = 'domain/view/$1';
$route['en/blog'] = 'domain/view/blog';
example.com/blog will work fine
but I need it to be more general to cover all pages except admin page, something like this:
$route['(:any)'] = 'domain/view/$1';
$route['^(?!admin).*'] = 'domain/view/$o';
//The above routes will show the home page only for whatever i try!!
What is the route that i have to add to routes.php?
$route['default_controller'] is invoked if there is no URI present. Use $route['404_override'] for a full "catch all."
To get your routing pattern working, try this:
$route['[^/]*/(.*)'] = 'en/domain/view/$1';
I have managed my problem by doing this
$route['^[a-z]+$'] = 'domain/view/$1';
$route['([a-z]{2})/([a-z_]{1,50})'] = 'domain/view/$2';

How do you use JRoute in Joomla to route to a Search menu item?

I am trying to create a a box in a template in Joomla! that will display all of the keywords and link them to their appropriate search page. I have a menu item set, however, I don't want to hard-code the menu item into the template, so I want to use the JRoute object to generate the SEF url.
I am using this function:
JRoute::_('index.php?option=com_search&searchword='.$keyword);
or this:
JRoute::_('index.php?option=com_search&view=search&searchword='.$keyword);
however, this generates a url like this:
/component/search/?searchword=africa
when it ought to create a search url like this:
/searchmenuitem?searchword=africa
I have searched extensivly online and havn't found a solution to this problem. Any ideas would be greatly appreciated.
Ok, so some additional information for you.. I am only experiencing the problem when I try and route the URL from a template in com_content. If I try and route the url from a template in com_search everything works perfectly. So, what is it about com_content that is causing this to not work properly?
thanks!
david
In joomla administration page go to the menu item you've chosen for the search results page and get the id of that menu item (itemId).
Than you can try using:
JRoute::_('index.php?option=com_search&view=search&Itemid=256&searchword=asdsadasdsa');
or even
JRoute::_('index.php?Itemid=256&searchword=asdsadasdsa');
both should result in: /searchmenuitem.html?searchword=asdsadasdsa
EDIT:
To make it more comforable you could add itemId as a param to your template.
There is another way, where u can get the itemId from the database (this method is required on multilingual websites). Let me know if you want it.
EDIT2:
Here it is:
$db =& JFactory::getDBO();
$lang =& JFactory::getLanguage()->getTag();
$uri = 'index.php?option=com_search&view=search';
$db->setQuery('SELECT id FROM #__menu WHERE link LIKE '. $db->Quote( $uri .'%' ) .' AND language='. $db->Quote($lang) .' LIMIT 1' );
$itemId = ($db->getErrorNum())? 0 : intval($db->loadResult());
I use this kind of method to get a menu item id of specific component and view
function getSearchItemId() {
$menu = &JSite::getMenu();
$component = &JComponentHelper::getComponent('com_search');
//get only com_search menu items
$items = $menu->getItems('componentid', $component->id);
foreach ($items as $item) {
if (isset($item->query['view']) && $item->query['view'] === 'search') {
return $item->id;
}
}
return false;
}
Then I use this method to get the sef url
function getRouteUrl($route)
{
jimport('joomla.application.router');
// Get the global site router.
$config = &JFactory::getConfig();
$router = JRouter::getInstance('site');
$router->setMode($config->getValue('sef', 1));
$uri = &$router->build($url);
$path = $uri->toString(array('path', 'query', 'fragment'));
return $path;
}
This just works in any template.
use like this
$itemid = getSearchItemId();
//returns valid sef url
$url = getRouteUrl('index.php?Itemid='.$itemid);
You really do not need to do sql on the menu table to get ids. Just search the menu object.
Try to create new menu in the joomla backend called for instance 'hidden-menu'. It will never be shown in the front. But it will be used by JRoute Then add to this menu new menuitem called 'searchmenuitem' with link to com_search. That is all. Now you can call
JRoute::_('index.php?option=com_search&view=search&searchword=asdsadasdsa');
and it will be ceonverted into this
/searchmenuitem.html?searchword=asdsadasdsa

Resources