codeigniter 2.1.3 multilanguage website how to? - codeigniter

codeigniter 2.1.3 multilanguage website how to ?
i have on /language/english and /language/french
this french
$lang['user_login'] = 'Connecté';
this english
$lang['user_login'] = 'Login';
the language file its called user_lang.php
on the controller contructor i have this :
$this->load->helper('url');
$this->load->helper('language');
on the function index i have this :
public function index()
{
$this->lang->load('user', 'french');
....
on the view i have the following :
<div class="pageTitle"><?php echo $this->lang->line('user_login');?></div>
/*
| -------------------------------------------------------------------
| Auto-load Language files
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['language'] = array('lang1', 'lang2');
|
| NOTE: Do not include the "_lang" part of your file. For example
| "codeigniter_lang.php" would be referenced as array('codeigniter');
|
*/
$autoload['language'] = array('user');
the question is that the website is working its going to the user_lang.php
to get the word login
but only goes to the english file
$this->lang->load('user', 'french');
iam doing this above , and its not working never reads the french file
i have another question , how can i get this working so i can switch languages with a anchor or href
thanks
Pedro

I would create a function like:
function language($language){
$language = urldecode($language);
switch($language){
case "French":
$this->session->set_userdata('lang_id', 2);
$this->session->set_userdata('lang_name', 'french');
redirect('/', 'refresh');
break;
case "Russian":
$this->session->set_userdata('lang_id', 3);
$this->session->set_userdata('lang_name', 'russian');
redirect('/', 'refresh');
break;
default: //default is English
$this->session->set_userdata('lang_id', 1);
$this->session->set_userdata('lang_name', 'english');
redirect('/', 'refresh');
break;
}
}
And I would change languages navigating to: http://www.domain.com/controller/language/French
Then in every controller or in constructor of each controller class I would check for the lang_id and lang_name session. If sessions are set, I would use them. Else the default language from the config will automatically load. For example:
$lang = $this->session->userdata('lang_name');
if(!isset($lang)){ //load default language
$this->lang->load('home');
$data = array(
'title' => lang('page_title')
);
//etc etc
}else{ //load language from session
$this->lang->load('home', $this->session->userdata('lang_name'));
$data = array(
'title' => lang('page_title')
);
//etc etc
}

in my welcome controller
public function french()
{
$this->session->set_userdata('lang_id', 2);
$this->session->set_userdata('lang_name', 'french');
$DContent['page_details'] = array('page_title' => 'Index of onplans');
$Dheader = array();
$Dsidebar = array();
$Dfooter = array();
$Dmeta = array('meta_title'=>'Welcome to onplans','meta_descricao'=>'onplans');
$this->template->write_view('meta', 'html/meta', $Dmeta, true);
$this->template->write_view('header', 'html/header', $Dheader, true);
$this->template->write_view('content', 'onplans/frenchset', $DContent,true);
$this->template->write_view('sidebar', 'html/sidebar');
$this->template->write_view('footer', 'html/footer');
$this->template->render();
}
public function english()
{
$this->session->set_userdata('lang_id', 3);
$this->session->set_userdata('lang_name', 'english');
$DContent['page_details'] = array('page_title' => 'Index of onplans');
$Dheader = array();
$Dsidebar = array();
$Dfooter = array();
$Dmeta = array('meta_title'=>'Welcome to onplans','meta_descricao'=>'onplans');
$this->template->write_view('meta', 'html/meta', $Dmeta, true);
$this->template->write_view('header', 'html/header', $Dheader, true);
$this->template->write_view('content', 'onplans/englishset', $DContent,true);
$this->template->write_view('sidebar', 'html/sidebar');
$this->template->write_view('footer', 'html/footer');
$this->template->render();
}
on the destination controller called user
$this->load->helper('url');
$this->load->helper('language');
print_r('lang_session'.$this->session->userdata('lang_name'));
$lang = $this->session->userdata('lang_name');
if(!isset($lang)){ //load default language
$this->lang->load('user');
}else{ //load language from session
print_r('lang :'.$this->session->userdata('lang_name'));
$this->lang->load('user',$this->session->userdata('lang_name')); //);
}
now its working

Related

Codeigniter with google oauth2 adds hashtag php to redirect('usercp')

I want to be able to redirect to another controller but when user logins in with google and is success full it gets redirected to there usercp but for some reason it gets the # from the end of here
http://www.example.com/test/google?code=4/sorrynocodeshown#
And when redirects using codeigniter redirect() it adds # to it.
http://www.example.com/usercp#
Question When redirecting to new page once successful login how to stop # from being added.
I use https://github.com/moemoe89/google-login-ci3
I also use vhost with xammp
Controller function
public function google() {
if ($this->input->get('code')) {
$googleplus_auth = $this->googleplus->getAuthenticate();
$googleplus_info = $this->googleplus->getUserInfo();
$google_data = array(
'google_id' => $googleplus_info['id'],
'google_name' => $googleplus_info['name'],
'google_link' => $googleplus_info['link'],
'image' => $googleplus_info['picture'],
'email' => $googleplus_info['email'],
'firstname' => $googleplus_info['given_name'],
'lastname' => $googleplus_info['family_name']
);
$login_google_userid = $this->login_model->login_with_google($googleplus_info['id'], $google_data);
$_SESSION['user_id'] = $login_google_userid;
redirect('usercp');
}
}
config/googleplus.php settings
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$config['googleplus']['application_name'] 'Somename';
$config['googleplus']['client_id'] = '*****';
$config['googleplus']['client_secret'] = '*****';
$config['googleplus']['redirect_uri'] = 'http://www.mysetname.com/account/login/google';
$config['googleplus']['api_key'] = '*****';
$config['googleplus']['scopes'] = array();
I am using HMVC with codeigniter
application/modules/account/controllers/Login.php
Full Controller
<?php
class Login extends MX_Controller {
private $error = array();
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('googleplus');
}
public function index() {
if ($this->login_model->is_logged_in()) {
$this->session->set_flashdata('success', 'Welcome back! If you wish to logout ' . anchor('account/logout', 'Click Here'));
redirect(base_url('usercp'));
}
if (($this->input->server("REQUEST_METHOD") == 'POST') && $this->validateForm()) {
$this->load->model('account/login_model');
$user_info = $this->login_model->get_user($this->input->post('username'));
if ($user_info) {
$_SESSION['user_id'] = $user_info['user_id'];
redirect(base_url('usercp'));
}
}
$data['login_url'] = $this->googleplus->loginURL();
if (isset($this->error['warning'])) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
if (isset($this->error['username'])) {
$data['error_username'] = $this->error['username'];
} else {
$data['error_username'] = '';
}
if (isset($this->error['password'])) {
$data['error_password'] = $this->error['password'];
} else {
$data['error_password'] = '';
}
// Common
$data['header'] = Modules::run('common/header/index');
$data['navbar'] = Modules::run('common/navbar/index');
$data['footer'] = Modules::run('common/footer/index');
$this->load->view('login', $data);
}
public function validateForm() {
$this->form_validation->set_rules('username', 'username', 'required');
$this->form_validation->set_rules('password', 'password', 'required');
if ($this->form_validation->run() == FALSE) {
$this->error['username'] = form_error('username', '<div class="text-danger">', '</div>');
$this->error['password'] = form_error('password', '<div class="text-danger">', '</div>');
}
if ($this->input->post('username') && $this->input->post('password')) {
$this->load->model('account/login_model');
if (!$this->login_model->verify_password($this->input->post('username'), $this->input->post('password'))) {
$this->error['warning'] = 'Incorrect login credentials';
}
}
return !$this->error;
}
public function google() {
if ($this->input->get('code')) {
$googleplus_auth = $this->googleplus->getAuthenticate();
$googleplus_info = $this->googleplus->getUserInfo();
$google_data = array(
'google_id' => $googleplus_info['id'],
'google_name' => $googleplus_info['name'],
'google_link' => $googleplus_info['link'],
'image' => $googleplus_info['picture'],
'email' => $googleplus_info['email'],
'firstname' => $googleplus_info['given_name'],
'lastname' => $googleplus_info['family_name']
);
$login_google_userid = $this->login_model->login_with_google($googleplus_info['id'], $google_data);
$_SESSION['user_id'] = $login_google_userid;
redirect('usercp');
}
}
}
Codeigniter's redirect() function uses the php header() function in 2 ways:
switch ($method)
{
case 'refresh':
header('Refresh:0;url='.$uri);
break;
default:
header('Location: '.$uri, TRUE, $code);
break;
}
using the refresh parameter will not add the hashtag.
You find more about this in system/helpers/url_helper.php
you can use this to your advantage in google_login.php changing
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
accordingly to
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Refresh:0;url=' . filter_var($redirect, FILTER_SANITIZE_URL));
When calling the redirect, you should be able to drop the hash by using the refresh param:
redirect('usercp', 'refresh');
You can modifying the url by doing something like
$url = strstr($url, '#', true);
But otherwise since it's a client-side stuff there's not a lot of options. You could also remove it from javascript when the client load the page with
history.pushState('', document.title, window.location.pathname + window.location.search)
since this is too long in the comment section, here goes:
try to use your browser's debug mode/developer tools, and see the network part of it. in there, you could see the sequence of requests when your page are loading.
if you are using chrome, thick the preserve log option before doing the oauth.
do the oauth and then try to find the request to google that redirects to your page.
click on the request, you will get the details of the request.
see for the response header, it should be 302 status and the destination should be your http://www.example.com/usercp url.
if you did not see the #, then you have problems in your part, try to check your .htaccess file.
if it's there in the destination, then the problem lies in google part, and not much you can do about it

joomla - router change url when getting the name of product

I have build my own component in joomla and client wants now a friendly urls f.e
website.com/someplace/{product-id}-{product-name}. So i Build my own router like this.
function componentBuildRoute(&$query)
{
$segments = [];
if (isset($query['view'])) {
$segments[] = "szkolenie";
unset($query['view']);
}
if (isset($query['product_id'])) {
$productName = JFilterOutput::stringURLSafe(strtolower(getProductName($query['product_id'])));
$newName = $query['product_id'] . '-' . $productName;
$segments[] = $newName;
unset($query['product_id']);
}
return $segments;
}
and parse route function
function componentParseRoute($segments)
{
$app = JFactory::getApplication();
$menu = $app->getMenu();
$item =& $menu->getActive();
$count = count($segments);
switch ($item->query['view']) {
case 'catalogue' : {
$view = 'training';
$id = $segments[1];
}
break;
}
$data = [
'view' => $view,
'product_id' => $id
];
return $data;
}
While on the end of buildroute function segments are ok I have exactly what I want that on the beginning of parse route I have something like
website.com/szkolenie/1-krakow <-- I dont know wtf is this krakow( I know it is city i Poland) but still where is it get from ? The getProductName function implementation is
function getProductName($productId)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('#__component_training.id as id, #__component_product' . name)
->from($db->quoteName('#__component_training'))
->where('#__s4edu_product.product_id = ' . $productId)
->leftJoin('#__component_product ON
#__component_training.product_id=#__component_product.product_id');
$training = $db->loadObject();
return trim($training->name);
}
So taking all this into consideration I think that something is happening between the buildRoute and parseRoute, something what filters the $segment[1] variable, but how to disable that and why is it happening ?
P.S
Please do not send me to https://docs.joomla.org/Joomla_Routes_%26_SEF
I already know all the tutorials on joomla website which contains anything with sef.
P.S.S
It is built on joomla 3.7.0
You do not have a product named "krakow" ?
If not you can try to remove the $productName from the build function, just to check if this "krakow" is added automaticaly or it's from the getProductName() function.
Also i noticed that you have an error i guess in the function getProductName()
->where('#__s4edu_product.product_id = ' . $productId)
It's should be
->where('#__component_product.product_id = ' . $productId)

How to Codeigniter HMVC Callback function error on live server?

AGood morning all.
I have build a website in codeigniter3 hmvc. all routes and callback function work perfectly on my localhost xampp and after uploading the project on godday share hosting live server I am facing callback funciton problems and below errors show in log file I don't understand why this happen. my user logging function also show this error:-
Unable to access an error message corresponding to your field name Password.(verifylogin)
Error Logs in logs folder of codeigniter
ERROR - 2015-09-16 01:30:08 --> 404 Page Not Found:
ERROR - 2015-09-16 01:30:29 --> 404 Page Not Found: /index
ERROR - 2015-09-16 01:30:30 --> 404 Page Not Found:
ERROR - 2015-09-16 01:31:30 --> Could not find the language line "form_validation_verifylogin"
ERROR - 2015-09-16 01:31:30 --> 404 Page Not Found: /index
These are my application/config/config.php
$config['index_page'] = "index.php?";
$config['uri_protocol'] = "QUERY_STRING";
$config['url_suffix'] = '';
$config['language'] = 'english';
Bellow is my application/config/routes.php setting
$route['Frontend/album_photos/(:num)/(:any)/(:num)'] = 'Frontend/album_photos/index/$1/$2/$3';
$route['Frontend/album_photos/(:num)/(:any)'] = 'Frontend/album_photos/index/$1/$2';
$route['Frontend/news_detail/(:num)/(:any)'] = 'Frontend/news_detail/index/$1/$2';
$route['(:any)/(:any)'] = 'Frontend/index/$1';
$route['(:any)'] = 'Frontend/index/$i';
$route['default_controller'] = 'Frontend/index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['Frontend/download_documents'] = 'Frontend/download_documents/$1';
//these routes works for admin panel
$route['Users/index'] = 'Users/index';
$route['Users/validate_login'] = 'Users/validate_login';
$route['Users/logout'] = 'Users/logout';
$route['Dashboard/index'] ='Dashboard/index';
$route['Aps_pages/manage'] ='Aps_pages/manage';
$route['Aps_pages/create'] ='Aps_pages/create';
$route['Aps_pages/submit'] ='Aps_pages/submit';
$route['Aps_pages/delete'] ='Aps_pages/delete';
$route['Aps_news/manage'] ='Aps_news/manage';
$route['Aps_news/create'] ='Aps_news/create';
$route['Aps_news/submit'] ='Aps_news/submit';
$route['Aps_news/delete'] ='Aps_news/delete';
$route['Aps_events/manage'] ='Aps_events/manage';
$route['Aps_events/create'] ='Aps_events/create';
$route['Aps_events/submit'] ='Aps_events/submit';
$route['Aps_events/delete'] ='Aps_events/delete';
$route['Document_upload/manage'] ='Document_upload/manage';
$route['Document_upload/create'] ='Document_upload/create';
$route['Document_upload/submit'] ='Document_upload/submit';
$route['Document_upload/delete'] ='Document_upload/delete';
$route['Aps_image_slider/manage'] ='Aps_image_slider/manage';
$route['Aps_image_slider/create'] ='Aps_image_slider/create';
$route['Aps_image_slider/submit'] ='Aps_image_slider/submit';
$route['Aps_image_slider/delete'] ='Aps_image_slider/delete';
$route['Aps_testimonials/manage'] ='Aps_testimonials/manage';
$route['Aps_testimonials/create'] ='Aps_testimonials/create';
$route['Aps_testimonials/submit'] ='Aps_testimonials/submit';
$route['Aps_testimonials/delete'] ='Aps_testimonials/delete';
$route['Aps_album_photo/manage'] ='Aps_album_photo/manage';
$route['Aps_album_photo/create'] ='Aps_album_photo/create';
$route['Aps_album_photo/submit'] ='Aps_album_photo/submit';
$route['Aps_album_photo/delete'] ='Aps_album_photo/delete';
$route['Aps_album_photo/search_result'] ='Aps_album_photo/search_result';
$route['Aps_photo_album/manage'] ='Aps_photo_album/manage';
$route['Aps_photo_album/create'] ='Aps_photo_album/create';
$route['Aps_photo_album/submit'] ='Aps_photo_album/submit';
$route['Aps_photo_album/delete'] ='Aps_photo_album/delete';
This is my Home.php Controller in Frontend Modules
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends MX_Controller
{
public $data =array();
function __construct()
{
parent::__construct();
$this->load->model('mdl_home');
$this->load->helper('site_helper');
$this->load->library('pagination');
}
public function index()
{
$slug = (string)$this->uri->segment(1);
//die($slug.'=is a slug');
$this->data['page'] = $this->mdl_home->get_pages(trim($slug));
// Fetch the page data
if(!empty($this->data['page'])){
$method = '_' .trim($this->data['page']->ViewTemplate);
if (method_exists($this, $method)) {
$this->$method();
}
else {
//die('nothing found method not found');
log_message('error', 'Could not load template ' . $method .' in file ' . __FILE__ . ' at line ' . __LINE__);
show_error('Could not load template ' . $method);
}
//some config to view out put
$this->data['view_file'] = $this->data['page']->ViewTemplate;
$this->data['module_name'] ='Frontend';
//these variable used for main page layout meta keywords description title
$this->data['page_title'] = $this->data['page']->Title;
$this->data['keywords'] = $this->data['page']->Keywords;
$this->data['description'] = $this->data['page']->Description;
$this->data['body'] = $this->data['page']->Body;
//load template module for front end
$this->load->module('Template');
$this->template->frontend_layout($this->data);
}else{
show_404();
}
}
//home page method
private function _home_page(){
$this->data['SliderList'] = $this->mdl_home->get_slider_list();
$this->data['NewsList'] = $this->mdl_home->get_news_with_limit('aps_news',12,'','','NewsDate DESC');
$this->data['EventsList'] = $this->mdl_home->get_event_list();
$this->data['TestimonialList'] = $this->mdl_home->get_testimonial_list();
$this->data['Documents'] = $this->mdl_home->get_uploaded_documents();
}
//this method is for static pages
private function _static_page(){
}
//this method load news page
private function _news_page(){
$table = 'aps_news';
$offset = ($this->uri->segment(2) != '' ? $this->uri->segment(2): 0);
$config['total_rows'] = $this->mdl_home->total_rows($table);
$config['per_page']= 20;
$config['uri_segment'] = 2;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = floor($choice);
$config['base_url']= site_url((string)$this->uri->segment(1));
$this->pagination->initialize($config);
$this->data['paginglinks'] = $this->pagination->create_links();
$this->data['NewsList'] = $this->mdl_home->get_news_with_limit($table,$config["per_page"], $offset,'NewsDate DESC');
}
//this method load contact page
private function _contact_page(){
}
//this method load events page
private function _events_page(){
$table = 'aps_events';
$offset = ($this->uri->segment(2) != '' ? $this->uri->segment(2): 0);
$config['total_rows'] = $this->mdl_home->total_rows($table);
$config['per_page']= 20;
$config['uri_segment'] = 2;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = floor($choice);
$config['base_url']= site_url((string)$this->uri->segment(1));
$this->pagination->initialize($config);
$this->data['paginglinks'] = $this->pagination->create_links();
$this->data['EventsList'] = $this->mdl_home->get_with_limit($table,$config["per_page"], $offset,'EventDate DESC');
}
//this method load photo album page
private function _album_page(){
$table = 'aps_photo_album';
$offset = ($this->uri->segment(2) != '' ? $this->uri->segment(2): 0);
$config['total_rows'] = $this->mdl_home->total_rows($table);
$config['per_page']= 12;
$config['uri_segment'] = 2;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = floor($choice);
$config['base_url']= site_url((string)$this->uri->segment(1));
$this->pagination->initialize($config);
$this->data['paginglinks'] = $this->pagination->create_links();
$this->data['AlbumList'] = $this->mdl_home->get_with_limit($table,$config["per_page"], $offset,'AlbumID DESC');
}
public function download_documents($file){
$this->load->helper('download');
$path = './assets/uploaded_documents/'.$file;
force_download($path, NULL);
redirect(base_url());
}
}
This is my Form Validation Library file
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation{
function run($module = '', $group = ''){
(is_object($module)) AND $this->CI = &$module;
return parent::run($group);
}
}
and this is my Users Controller file
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Users extends MX_Controller
{
public function __construct()
{
parent::__construct();
//load financial year
$this->load->model('mdl_users');
}
public function index()
{
if($this->session->userdata('logged_in'))
{
redirect('Dashboard');
}else
{
$this->login();
}
}
//this function used to show login form view in template one column layout view
public function login()
{
$data['view_file'] = "loginform";
$data['module_name'] ='Users';
$data['page_title'] = 'Login Page';
$this->load->module('Template');
$this->template->login_layout($data);
}
//after entering the login values login form submit value handled by this function or method
public function validate_login()
{
$this->form_validation->set_error_delimiters('<div style="color:red;">', '</div>');
$this->form_validation->set_rules('username', 'Username', 'required|max_length[30]|xss_clean');
$this->form_validation->set_rules('password','Password', 'required|callback_verify_login');
if ($this->form_validation->run($this) == FALSE)
{
$this->index();
}
}
//this function for callback validation after submit button press
public function verify_login() {
$username = $this->input->post('username');
$password = $this->input->post('password');
//this method is in model folder/directory which interact with database https://www.youtube.com/watch?v=8fy8E_C5_qQ
$user = $this->mdl_users->check_login_authentication($username, $password);
if(empty($user)){
$this->form_validation->set_message('verify_login', 'Sorry the details you provided have not been found');
return FALSE;
}else{
//set user data in session and redirect to dashboard
$data = array(
'username' =>$user->UserName,
'userid' => $user->UserID,
'logged_in' => TRUE
);
$this->session->set_userdata($data);
redirect('Dashboard');
}
}
//logout function
public function logout()
{
$this->session->sess_destroy();
redirect('Users');
}
}

Error 500 "View Not Found" when using SEF and URL Rewriting

I am writing a custom component. I have the view employees. Under this view, I have two layouts, default and modal.
I have a menu item in the toplevel of the main menu, Employees that points to my employee view:
index.php?option=com_mycomponent&view=employees which resolves to domain.com/joomla/employees and displayes the default view as expected.
Now, inside my component I want to link to the modal view, and I do so using JRoute and this url:
index.php?option=com_mycomponent&view=employees&layout=modal
Which resolves to
domain.com/joomla/employees/modal
And produces this error:
500 - View not found [name, type, prefix]: modal, html,
mycomponentView
If I visit index.php using index.php?option=com_mycomponent&view=employees&layout=modal my modal view is displayed.
I have also found that visiting domain.com/joomla/employees/employees/modal displays the correct layout. It is as if joomla is forgetting what view is associated with the menu item at /joomla/employees, and instead looks for the view "modal" unless the extra "employees" is provided in the url.
Also worth noting, domain.com/joomla/employee?layout=modal works fine as well.
Here is what I have for my router.php. This was file was generated for me using the component generator at j-cook.pro.
<?php
defined('_JEXEC') or die;
function MycomponentBuildRoute(&$query){
$segments = array();
if(isset($query['view']))
{
$view = $query['view'];
$segments[] = $view;
unset( $query['view'] );
}
if(isset($query['layout']))
{
$segments[] = $query['layout'];
unset( $query['layout'] );
}
if(isset($query['id']))
{
if(in_array($view, array('edit','view','view','editfacility','view','edit','client','editclient','viewposition','editposition','edit','view','edit','view','view','edit','view','edit','view','edit','view','edit')))
{
$segments[] = (is_array($query['id'])?implode(',', $query['id']):$query['id']);
unset( $query['id'] );
}
};
return $segments;
}
function MycomponentParseRoute($segments)
{
$vars = array();
$vars['view'] = $segments[0];
$nextPos = 1;
if (isset($segments[$nextPos]))
{
$vars['layout'] = $segments[$nextPos];
$nextPos++;
}
if(in_array($vars['view'], array('edit','view','view','editfacility','view','edit','client','editclient','viewposition','editposition','edit','view','edit','view','view','edit','view','edit','view','edit','view','edit'))
&& isset($segments[$nextPos]))
{
$slug = $segments[$nextPos];
$id = explode( ':', $slug );
$vars['id'] = (int) $id[0];
$nextPos++;
}
return $vars;
}
So it is hard to provide an exact answer for this without knowing all the different ways that you want to have urls be parsed. But I will try to give a hint at what will solve this present situation (without hopefully introducing too many new issues!)
The basic issue is that the BuildRoute side does not get a view value so it is not included in the url. On the one hand it is not necessary, because it is in the menu. But it makes it a little harder to parse, so option one is to force there to be a view if you can by changing the top function to start like this:
function MycomponentBuildRoute(&$query){
$segments = array();
if(isset($query['view']))
{
$view = $query['view'];
$segments[] = $view;
unset( $query['view'] );
}
else
{
$app = JFactory::getApplication();
$menu = $app->getMenu();
$active = $menu->getActive();
if ($view = $active->query['view']) {
$segments[] = $view;
}
}
...
In this way, if there is a menu item for this and it has a view we will tack it on. This should generate domain.com/joomla/employees/employees/modal.
You could also probably do this logic on the parse side too. This would go instead of the other option above:
function MycomponentParseRoute($segments)
{
$vars = array();
$app = JFactory::getApplication();
$menu = $app->getMenu();
$active = $menu->getActive();
if ($active->query['view']) {
$vars['layout'] = $segments[0];
$nextPos = 1;
} else {
$vars['view'] = $segments[0];
$nextPos = 1;
if (isset($segments[$nextPos]))
{
$vars['layout'] = $segments[$nextPos];
$nextPos++;
}
}
... continue with final check for id
I would probably use the second option but both are an option. By the way, you are also likely to run into issues if you try to use an id without setting a layout.

codeigniter pagination error 404

I have Admin controller in codeigniter
class Admin extends CI_Controller {
function __construct() {
parent::__construct();
if (!$this->tank_auth->is_logged_in()) redirect('login');
$this->load->library('pagination');
}
function index() {
$offset = $this->uri->segment(2);
$config['per_page'] = 3;
$data['sitetitle'] = 'Výpis jobů';
$data['listings'] = $this->Jobs_model->get_listings(0,$user_id = FALSE,$config['per_page'],$offset);
$config['uri_segment'] = 2;
$config['base_url'] = base_url().'admin/';
$config['total_rows'] = $this->db->count_all_results('jobs');
$this->pagination->initialize($config);
$this->template->set('title', 'Domovská stránka');
$this->template->load('template', 'site', $data);
}
}
and Jobs_model
function get_listings($category, $user_id = false, $limit = 0, $offset = 0) {
$data = array();
$this->db->order_by('id', 'desc');
$q = $this->db->get('jobs');
if ($category) {
$options = array('category' => $category);
$this->db->order_by('id', 'desc');
$this->db->where('category', $category);
$q = $this->db->get('jobs', $limit, $offset);
}
else {
$query = $this->db->order_by('id', 'desc');
if ($user_id) $query = $query->where('user_id', $user_id);
$q = $query->get('jobs',$limit, $offset);
}
if ($q->num_rows() > 0) {
foreach ($q->result_array() as $row) {
$data[] = $row;
}
}
$q->free_result();
return $data;
}
first page in paginatiom obtain data, but links generating in pagination localhost/sitename/admin/3 produce 404 error.
Where is problem in my script
You need to change:
$config['base_url'] = base_url().'admin/';
To:
$config['base_url'] = base_url().'admin/index/';
If you need the url to be like admin/3, you can use a route or _remap.
Side note: consider using this to get your page number rather than the URI class:
function index($offset = 0) {
// your code
}
It will do the same thing, but it's convenient to use the controller method arguments when possible.
You need to do some change.
$config['base_url']=base_url().'admin/index';
change
$config['uri_segment'] = 3;
check if you are using .htaccess or not. If you aren't, then $config['base_url'] in above should be
$config['base_url']=base_url().'index.php/admin/index';
If you get 404 error on codigniter paggination please check follwing things :
1 ) Check your controller it's index or somthing else if it's index then base_url should be :
base_url().'admin/index/pages' and if it's somethings else it should be :
base_url().'admin/somethingelse/pages'
2 ) Check 'uri_segment' by $this->uri->segment(4) so by this you got the page number then else will do the codiegniter.

Resources