How to route everything to default controller in codeigniter? - 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';

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;

how to make codeigniter routing to work with paging

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;

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.

CodeIgniter: Multilanguage when controllers hidden

Please help me. My situation is when I hide the index.php and controllers name on url like this:
localhost/ctc/index.php/controllers/function/ ==> localhost/ctc/function
It work fine and after that, I want multilanguage in my site and use tutorial from this site: http://maestric.com/doc/php/codeigniter_i18n, my url turn to:
localhost/ctc/function ==> localhost/ctc/en/function or localhost/ctc/fr/function
Problem is, when I change language from france to english, the current page turn to english (of course), but when I click to other pages, language come back to default language is france and I can't figure out why.
This is my routes.php:
$route['default_controller'] = "main/main_page";
$route['en/main/(:any)'] = "main/$1";
$route['fr/main/(:any)'] = "main/$1";
$route['en/(:any)'] = "main/$1";
$route['fr/(:any)'] = "main/$1";
$route['(:any)'] = "main/$1";
$route['^(en|fr)/(.+)$'] = "$1";
$route['^(en|fr)$'] = $route['default_controller'];
$route['404_override'] = '';

Default Controller not loading after rerouting

I have a codeigniter multistie install where I have such code that I can serve sites with such links
site.com/sites/sitename/controller/function/vars
site.com/controller/function/vars/
subdom.site.com/controller/function/vars
the challange is that , whith the routing
$route['sites/([^/]+)/(.*)'] = '$2';
$route['default_controller'] = "content";
I get working the links like
site.com/sites/sitename/controller/function/vars
site.com/controller/function/vars
By idea when I go to
www.site.com/sites/sitename/
the default controller is not loading.
I made the config.php so that regarding the link structure, when I visit link
site.com/sites/sitename/controller/function/vars
then
$config['base_url']="http://site.com/sites/sitename";
if I go to
site.com/controller/function/vars
then
$config['base_url']="http://site.com/";
for the second case the default controller loads perfectly. For the subsite case not/
I get just 404
What to do?
UPDATE 2:
I have a multifolder setup.
When user goes to www.site.com/sites/site_name
then a folder of application
/root_folder/usersites/site_name is loaded.
When user goes just site.com/controller/function/var1/var2
a default application folder which is
/root_folder/application is loaded
when user goes to sub1.site.com application folder
/root_folder/domains/sub1_site_com is loaded
So when I enter in address bar
http://site.com/sites/site_name
it should be like no URI. and should load default controller.
// Application folder loading code
$myApp = '';
if($_SERVER['SERVER_ADDR']=='127.0.0.1'){
$main_url='site.com';
}
else{
$main_url='site1.com';
}
//echo $main_url;
switch($_SERVER['HTTP_HOST'])
{
case $main_url;
$uri_string=$_SERVER['REQUEST_URI'];
$link_way=explode('/',$uri_string);
if(strlen($uri_string)>6 and $link_way[1]=='sites' ){
//print_r($link_way);
//var_dump($link_way);
//checking if the link goes to usersites and sitename is bigger and =5
if($link_way[1]=='sites' and strlen($link_way[2])>=5){
$myApp='sites/usersites/'.$link_way[2];
define('SITE_ALIAS','1|'.$link_way[2]);
}
elseif($link_way[1]=='sites' and strlen($link_way[2])<5){
exit('Username should be more than 4 chars');
}
}
else{
define('SITE_ALIAS','0|'.str_replace('.','_',$_SERVER['HTTP_HOST']));
$myApp = 'application';
}
break;
default:
$myApp = str_replace('.','_',$_SERVER['HTTP_HOST']);
$myApp=str_replace('www_','',$myApp);
define('SITE_ALIAS','2|'.$myApp);
$myApp='sites/domains/'.$myApp;
}
$application_folder = $myApp;
What you appear to be doing is looking for a controller with the 'sitename' you are passing through. So if you navigate to site.com/sites/my-site/ you route tells it to look for a controller called my-site and run the index method.
The value of the route should be a path to an actual controller / method pair.
$route['sites/([^/]+)/(.*)'] = '$2';
should be
$route['sites/([^/]+)/(.*)'] = 'sites/index/$1/$2';
This is assuming it's the index method that accepts the sitename as it's first parameter in your sites controller.
It's not totally clear what you're asking, but heres a shot:
You need to create an .htaccess file in the root of your site (i.e. in the same folder that your system folder is in). In that file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
See the "Remove the index.php file" section of this page: http://codeigniter.com/user_guide/general/urls.html for more info.
Also, the route that you're using would make it so that when you go to www.site.com, you will see www.site.com/sites/content.
With the url www.site.com/sites/sitename/content/, sites is your controller, sitename the method or function, and content would be considered a parameter to the sitename function -- this won't work like it seems like you want, but I can't be sure without seeing your controller.
Maybe edit your question and add your controller(s), and we can be of more assistance.
UPDATE:
1: $config['base_url'] has nothing to do with routing or which controller is being used, so this is making your question harder to understand.
2: It isn't clear what you are trying to accomplish (sorry).
By idea when I go to
www.site.com/sites/sitename/
the default controller is not loading.
According to the CI user guide:
CodeIgniter can be told to load a
default controller when a URI is not
present, as will be the case when only
your site root URL is requested. To
specify a default controller, open
your application/config/routes.php
file and set this variable:
So, what this means is that the default_controller is used only when there is no URI present. In other words: the default controller only applies when the URL is www.site.com, and in no other case will it be used (unless you are using sub-folders in the controllers folder -- see below).
If you trying to make it so that each of your sites has its' own controller, you could use subfolders in your controller folder.
In routes.php:
$route['sites/(:any)'] = "$1";
$route['default_controller'] = "content";
Then your folder structure:
So you have your controller folder. In it, create a folder for each site. In each of those controllers create your default controller (named content.php in the above image).
With this setup, www.site.com/sites/site1 will call the default controller (content) from application/controllers/site1/content.php and show the index function of that controller.
If you then want to call other functions of the site1 controller, the URL would look like:
www.site.com/sites/site1/content/otherFunction.
Hope this helps.
$uri_string=$_SERVER['REQUEST_URI'];
$way=explode('/',$uri_string);
/// print_r($way);
//echo $way[3];
if($way[1]=="sites" and strlen($way[2])>2 and strlen($way[3])<1){
echo "JAJA ";
$route['sites/:any'] = "content/show_page/home";
}
else{
$route['sites/([^/]+)/(.*)'] = '$2';
}
this was solution. thanks all who answered. thanks stormdrain. you pointed me to a write direction in your routing example. Thanks

Resources