We're using Codeingiter library i18n (link) to create a multilingual site.
Before this, we had for example www.thedomain.com/register and register was a function in our controller. Now when we put this library it grabs the domain, the language string, the controller's name and the functions name: www.thedomain.com/es/homegf/register (where homegf is our controller).
We want this URI's to work without the name of our controller on it (www.thdomain.com/es/register) like in the librarie's examples but we think the problem is in our routes.php.
This is what we have in routes.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$route['default_controller'] = "homegf";
$route['404_override'] = '';
$route['^(en|es|de)/(.+)$'] = "$2";
$route['^(en|es|de)$'] = $route['default_controller'];
This is our .htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|files|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
You can find our code to review it at https://bitbucket.org/ticketcomunicacion/grinfood/src/17ddde60e340a1f2bc389f54ec579e1e903ee86b?at=multilenguaje
as i understand , you want the controller url without controller name .
if you want to change the URL from your controller name to other ,
in your routes.php write the following
$route['homegf/register'] = "register";
and you can access
domain.com/es/register
The problem was in our routes.
This is how we managed to make it work:
$route['^es/(.+)$'] = "homegf/$1";
$route['^en/(.+)$'] = "homegf/$1";
$route['^es$'] = $route['default_controller'];
$route['^en$'] = $route['default_controller'];
$route['(:any)'] = 'homegf/$1';
Related
I'm using codeigniter. How can I get last part of my URL like:
www.example.com/uk-en/category/subcategory
Parameters may be unlimited or there may be only one. Inside of my Controller Home and method Index, I just want last part of url e.g "subcategory"
if url is www.example.com/uk-en/category I want "category"
if url is www.example.com/uk-en I want "uk-en"
Edit
if url is www.example.com/Home/index/uk-en/category then it's working
but What i want without class name "Home" and method "index"
Like this www.example.com/uk-en/category
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller{
public function index($params=[]){
$params=func_get_args();
$last=end($params);
echo $last;
}
}
?>
routes.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['(:any)'] = 'Home/index';
$route['default_controller'] = 'Home';
$route['404_override'] = 'error_404/index';
$route['translate_uri_dashes'] = FALSE;
?>
.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
Use this in your routs
$route['(.*)'] = "Home/index";
This to print all routs in your controller in your index function
print_r($this->uri->segment_array());
Problem in your routes is with setting wildcard placeholder on first place. It always need to be at last place.
Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.
$route['default_controller'] = 'Home';
$route['404_override'] = 'error_404/index';
$route['translate_uri_dashes'] = FALSE;
//some example routes
$route['specific/route'] = 'controller/method';
$route['specific/(:num)'] = 'page/show/$1';
//always put your wildcard route at the end of routes.php file
$route['(:any)'] = 'home/index';
Docs.
When I set $route['default_controller'] to 'blog', CI opens my controller without problems. When I want to access it via URL, CI returns a 404.
My controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Blog extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
Saved at
application/controllers/Blog.php
Some other config things:
$route['default_controller'] = 'blog';
$route['404_override'] = '';
$config['base_url'] = '';
URL I try to use:
http://localhost:63342/project/public_html/blog/
.htaccess to hide index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
How can I fix this issue? I tried a lot of solutions from similar questions without success
If your project is in project folder in main directory of the local server, change your address to:
http://localhost:63342/project/blog
You configured that page as your default controller, so it should work even by:
http://localhost:63342/project
This is the error that i am getting.
Please make sure the controller specified in your Routes.php file is valid.
Let me explain this.
In my application i have used HMVC. In my application folder there are two folders back-modules and front-modules.in my front-modules folder there is a folder called "home" and it has controllers,modules and views.
controllers->home.php
this is my home.php
class Home extends MX_Controller{
public function __construct(){
parent::__construct();
$this->load->helper(array('form'));
}
public function index(){
$this->template->build('home');
}
}
and this is my routes.php
$route['default_controller'] = "home";
$route['404_override'] = '';
when i try to go to the site, it gives this error
unable to load your default controller on Codeigniter.Please make sure the controller specified in your Routes.php file is valid.
this system works fine on localhost.
Someone please help me in this matter.
Typically there is a one-to-one relationship between a URL string and its corresponding controller class/method. The segments in a URI normally follow this pattern:
example.com/class/function/id/
your 'default_controller' should be:
$route['default_controller'] = "home/index";
read official CodeIgniter URI Routing Documentation
Kindly Check .htaccess file exist or not in root place of your website.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
Some times codeigniter won't find the controller.
Go to config > routers.php
and see the value of default_controller
make a controller in controllers folder with the same value.
According to codegniter manual managing multi application
in your index.php file change
$application_folder = 'application';
to
$application_folder = 'application/home';
and make sure the controller file name starts with capital letter
controllers->Home.php
and keep Routes.php configurations
$route['default_controller'] = "home";
I am trying to create custom routes for my CodeIgniter site. Even the most basic routes do not work. For example I have the welcome controller mapped to "test" and it just 404's on me. I am running on MAMP with mod_rewrite enabled.
I have the index.php line in config.php empty..
$config['index_page'] = '';
Here is my .htacess file..
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
And here is my route..
$route['welcome'] = 'test';
In a route, the array key contains the URI to be matched, while the
array value contains the destination it should be re-routed to.
-- CI Documentation
Your physical controller name is welcome. So if you want a URL containing the word test in the first segment be remapped to the welcome class, you should do this:
$route['test'] = "welcome/METHOD";
Where METHOD is method of welcome class.
Note: If class/method was welcome/index, you do NOT need to append /index.
If I read this correctly, that route will try to redirect the controller 'welcome' to the controller 'test'. If you have a controller named 'test' and a function named 'index', you can route the following:
route['welcome/index'] = 'test/index';
Is that what you are trying to do?
A couple things:
Routes in CI are cascaded, they're evaluated top to bottom, and the router stops at the first match. Make sure, then, to have any custom route placed below the 2 default routes in a vanilla distribution:
$route['default_controller'] = "welcome";
$route['404_override'] = '';
// custom routes here
$route['welcome'] = "welcome/test";
If you have a controller named "welcome" (the default one), and you want to call a method named "test", you need a route like
$route['welcome'] = "welcome/test"
which will be accessible at the url http://wwww.yourdomain.com/welcome
(if no route were specified, you would have accessed it like http://www.yourdomain.com/welcome/test)
Usually, controller have an index method which is called automatically when no other method is provided. The route you've created so far isn't working because it's calling the index() method of a "test" controller, which is likely not present.
A suggestion: if you mainatain the "welcome" controller as the default one, and you want to call an url like http://www.yourdomain.com/test
You need a test() method and your route must be
$route['test'] = "welcome/test";
How to rewrite particular url of website using .htaccess or CI routing
http://www.domain.com/user_controller/newfunction/username
as
http://www.domain.com/username
$route['/(:any)'] = "/user_controller/newfunction/$1";
or try htaccess
RewriteEngine On
RewriteRule ^([^/]*)$ /user_controller/newfunction/$1 [L]
/application/config/routes.php
$route['/all/other/routes/must/come/first!'] = "wherever/you/want";
$route['/(:any)'] = "/user_controller/newfunction/$1";
Its easier to use CI routing system found at application/config/routes.php
$route['(:any)'] = 'user_controller/newfunction/$1';
Don't forget to set your base url at application/config/config.php
$config['base_url'] = 'http://www.domain.com/';
Add remember to remove the index.php in the url. Edit your .htaccess file at the root of your site
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|_searches|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
now in your user_controller, the function newfunction will look like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class user_controller extends CI_Controller {
public function __construct() {
parent::__construct();
// load things like models, libraries, helpers
}
function newFunction($slug)
{
echo 'hello my name is'.$slug;
}
}
when you visit http://www.domain.com/nash, your code should print out nash