Put language index into URL Multilanguage CodeIgniter site - php

My site is created in three languages https://anto-nguyen.com.
It works well to translate from one language to another one.
But.. I can't put the language indication into the URL.
I looked through all questions and answers that I could find here and have tried to use them, but unfortunatelly it doesn't work..
I use Controller LangSwitch
class LangSwitch extends CI_Controller {
public function __construct() {
parent::__construct();
}
function switchLang($language = "") {
$this->session->set_userdata('site_lang', $language);
redirect($_SERVER['HTTP_REFERER']);
}
}
The language is called by following code integrated into menu
<div>
<?= anchor(base_url("langSwitch/switchLang/english"), 'En'); ?>
<?= anchor(base_url("langSwitch/switchLang/french"), 'Fr'); ?>
<?= anchor(base_url("langSwitch/switchLang/russian"), 'Ру'); ?>
</div>
My .htaccess lookes like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /codeigniter/index.php/$1 [L]
And seems to be modified, but all suggestions I found doesn't work
Could you help me what to do that my link will appeared as /mysite/fr /mysite/en /mysite/ru?
All translation are in the folders language/english language/french language/russian
Mayby to change something in routes?
$route['default_controller'] = 'site';
$route['work'] = 'work/index'; //the URL 'work' will redirect to 'work/index'
$route['work/(:any)_(:num)'] = 'work/article/$2';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['(:any)'] = 'site/$1';

Related

404 with CodeIgniter on form submit

I found this has been asked by many. But none of the answers could resolve my issue. So posting a new thread.
controllers\login.php
class Login extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index(){
$this->load->view('login/index');
}
public function process(){
echo 'hi';
}
}
views\login\index.php
<?php echo form_open('login/process'); ?>
<button type="submit" >OK</button>
<?php echo form_close(); ?>
config\config.php
$config['base_url'] = 'http://localhost/aiop/';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
root .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php
when I launch the application using http://localhost/aiop/ it is launching the login/index.php. When I click on button on the page, it is trying to navigate to http://localhost/aiop/login/process and says 404
Ah At last I figured it out. The application folder name was in a different case (AIOP) than what is defined in config (aiop). Though this seems silly answer, I thought it could help someone who came from non-case sensitive world (Windows/.NET)

Codeigniter subdomain routing

I'm trying to setup a blog script on a website running on the CodeIgniter framework. I want do this without making any major code changes to my existing website's code. I figured that creating a sub domain pointing to another Controller would be the cleanest method of doing this.
The steps that I took to setup my new Blog controller involved:
Creating an A record pointing to my server's ip address.
Adding new rules to CodeIgniter's routes.php file.
Here is what I came up with:
switch ($_SERVER['HTTP_HOST']) {
case 'blog.notedu.mp':
$route['default_controller'] = "blog";
$route['latest'] = "blog/latest";
break;
default:
$route['default_controller'] = "main";
break;
}
This should point blog.notedu.mp and blog.notedu.mp/latest to my blog controller.
Now here is the problem...
Accessing blog.notedu.mp or blog.notedu.mp/index.php/blog/latest works fine, however accessing blog.notedu.mp/latest takes me to a 404 page for some reason...
My .htaccess file looks like this (the default for removing index.php from the url):
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
And my Blog controller contains the following code:
class Blog extends CI_Controller {
public function _remap($method){
echo "_remap function called.\n";
echo "The method called was: ".$method;
}
public function index()
{
$this->load->helper('url');
$this->load->helper('../../global/helpers/base');
$this->load->view('blog');
}
public function latest(){
echo "latest working";
}
}
What am I missing out on or doing wrong here? I've been searching for a solution to this problem for days :(
After 4 days of trial and error, I've finally fixed this issue!
Turns out it was a .htaccess problem and the following rules fixed it:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Thanks to everyone that read or answered this question.
Does blog.domain.co/blog/latest also show a 404?
maybe you could also take a look at the _remap() function for your default controller.
http://ellislab.com/codeigniter/user-guide/general/controllers.html#default
Basically, CodeIgniter uses the second segment of the URI to determine which function in the controller gets called. You to override this behavior through the use of the _remap() function.
Straight from the user guide,
If your controller contains a function named _remap(), it will always
get called regardless of what your URI contains. It overrides the
normal behavior in which the URI determines which function is called,
allowing you to define your own function routing rules.
public function _remap($method)
{
if ($method == 'some_method')
{
$this->$method();
}
else
{
$this->default_method();
}
}
Hope this helps.
have a "AllowOverride All" in the configuration file of the subdomain in apache?
without it "blog.notedu.mp/index.php/blog/latest" work perfectly, but "blog.notedu.mp/latest" no
$route['latest'] = "index";
means that the URL http://blog.example.com/latest will look for an index() method in an index controller.
You want
$route['latest'] = "blog/latest";
Codeigniter user guide has a clear explanation about routes here

URI ROUTING in CodeIgnitor_2.1.3 throwing "Not Found" error

I have installed CodeIgniter_2.1.3 and running in
Windows 7
Wamp Server 2.1
PHP 5.3.5
Apache 2.2.17
In \applicationconfig\routes.php, I created
$route['default_controller'] = "welcome";
$route['404_override'] = '';
$route['products/catlog'] = "welcome/getOneMethod";
And in \application\controllers\welcome.php, I created a method
public function index()
{
$this->load->view('welcome_message');
}
public function getOneMethod()
{
echo "hi im in newMethod";
}
http://localhost/CodeIgniter_2.1.3/products/catlog
Now I expect on running this url above on browser to give me the page
hi im in newMethod
But instead i'm getting the error message.
Not Found
The requested URL /CodeIgniter_2.1.3/products/catlog was not found on
this server.
What should i do to make it work correctly?
Doo you visit codeigniter documentation website,You can get help from this url how to create static pages
http://ellislab.com/codeigniter/user-guide/tutorial/static_pages.html
For the given url routes and controller.
you will need to use:
http://localhost/CodeIgniter_2.1.3/index.php/products/catlog/
The index.php part in the url can be removed using htaccess rewrite rule.
In the /codeigniter2.1.3/ folder create an .htaccess file and put the following rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /CodeIgniter_2.1.3/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /CodeIgniter_2.1.3/index.php [L]
</IfModule>
Note: This might not be the best way to do it.
EDIT:
Make sure the controller is exactly as bellow,
class Welcome extends CI_Controller {
public function index() {
$this->load->view('welcome_message');
}
public function getOneMethod() {
echo "hi im in newMethod";
}
}
and the routes are as bellow:
$route['default_controller'] = "welcome";
$route['404_override'] = '';
$route['products/catlog'] = "welcome/getOneMethod";
For the time being remove the htacces, and make it work with index.php in url.
ie:
http://localhost/CodeIgniter_2.1.3/index.php/products/catlog/
Note: since you are able to see the welcome message, there is no problem with your installation or server. there is probably some syntax error. so i recommend you copy paste the above code, as i have checked them and they are working.

Url rewriting for codeigniter

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

codeigniter redirect from a view

I am building a site entirely on codeigniter .I have set my default controller as cuff.
so whenever users type the domain name it takes the control to that controller.
class Cuff extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
$this->load->view('index');
}
public function navigate()
{
echo "test";
exit;
}
}
In my index view I want an anchor to navigate to a function in my default controller .
so when I write
our collection , it says page not found .
I have even set the base url like this
$config['base_url'] = "http://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= preg_replace('#/+$#','',dirname($_SERVER['SCRIPT_NAME'])).'/';
Cant seem to figure out the issue.
You missed the controller name
our collection
Set $config['base_url'] ='';
In your .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|js|css|favicon|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]
Make sure your controller is in controller's folder (not any subfolder) and is properly named cuff.php
Try navigating to http://yoursite/cuff/navigate
If you still see errors try rewriting the last line in .htaccess to:
RewriteRule ^(.*)$ /index.php/$1 [L]
(without question mark).
Please tell if that helps!
EDIT: and your links better all be in this form:
Link
The problem is in your routing.
Edit your routes.php (located in the folder ../application/config/) and add the code below:
$route['navigate'] = "Cuff/navigate";
For more information regarding Codeingiter URI Routing:
http://codeigniter.com/user_guide/general/routing.html
Hope this helped you.
Simply you use
"<?php echo site_url();?>/cuff/navigate">our collection</a>

Categories