I have a multi language website in code igniter with uses prefix in the URL to define the language. Works great but there is a problem when going to the home page.
The default URL is
localhost:8888
but it should be
localhost:8888/index.php/EN/welcome
I tried redirecting in the controller but that didn't work.
Any ideas on how to fix this problem?
Thanks a lot
First of all you must create a .htaccess file in your root path. File content like this;
Options -Indexes
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
You can edit your route file like this;
$route['(:any)/(:any'] = 'IndexController/getPage/$1/$2';
$route['(:any)'] = 'IndexController/index/$1';
$route['default_controller'] = 'IndexController';
Route 1: Your sub page. First parameter is language, second parameter is page url
Route 2: Your home page. Parameter is language.
Route 3: Default home page in your main language.
Related
I believe that I already did all the important thing to set up the custom route in codeigniter but I still don't know why I always get 404 error.
The current url I can access is: http://localhost:8080/project/api/profile_test/
And I want to rewrite it as: http://localhost:8080/project/api/users/
I've added this code in the route.php but still not working: $route[‘users’] = 'profile_test';
My .htaccess is:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
I already set the AllowOverride All and remove the index.php in $config['index_page'] = '';. But still not working.
I’m almost 8 hours for this with this little thing and I need help to the one who knows it.
What am I missing? Any help is much appreciated.
In Codeigniter, a route consists of 3 parts:
the URL
the controller name
the method name
The docs explain it well.
So, if your project's root is http://localhost:8080/, for a URL like project/api/users/, to be handled by a controller (located in application/controllers/) called Profile_test and a method called index, you would need a route like:
$route['/project/api/users'] = 'profile_test/index';
Note that the controller file and class must be capitalised, as described in the docs, so for example controllers/Profile_test.php.
If your project's root is http://localhost:8080/project, you would change that to:
$route['/api/users'] = 'profile_test/index';
If your controller is located in a subdirectory like application/controllers/api/, you would change that to:
$route['/project/api/users'] = 'api/profile_test/index';
Also make sure you are using normal single quotes. Some of the code you include in your question includes 'smart' quotes: $route[‘users’] which will not work in PHP.
I just started learning codeigniter and came up with some problem I could not sort.
In routing the default controller = 'home'
the base_url()=localhost/CodeIgniter_2.1.3/:
So when the site is loaded the site_url()=localhost/CodeIgniter_2.1.3/index.php
Home controller contains a link , to register controller.
So when this register controller is inside home object, and when the is linked to localhost/CodeIgniter_2.1.3/index.php/register it works fine.
But I want to make my Register controller a different object so that, I can go to register page like this localhost/CodeIgniter_2.1.3/register which I cannot get done. I tried messing with 'routes' but no luck there. Any Ideas?
create in your root directory a .htaccess file with following rules to use URI segments on routing properly :
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
more at codeigniter documentation
I recently started using codeIgniter and I am stuck in a problem.
I've two files in my view folder
index.php
profile.php
When I go to the url http://localhost/php/ci/index.php/ it shows my index.php page and everything is fine.
But when I go to http://localhost/php/ci/profile.php/ it says
The requested url was not found on server.
Why is this happening what's the problem ??
My Controller Files Names Are:
home_control.php
profile_control.php
home_control will interact with index.php and profile_control will interact with profile.php.
You have a fundamental flaw in your MVC understanding. The index.php file sitting in your document root (that is, your www directory) is actually responsible for getting CodeIgniter started on every request. That is the index.php that you are seeing in the URL bar.
You are confusing said index.php with the index.php that you fabricated inside of the views folder. Never will you be able to create a file inside of your views folder and be able to immediately access it using the URL bar. You must go through a controller.
If you access http://localhost/php/ci/index.php/profile_control and controllers/profile_control.php contains::
<?php
class Profile_control extends CI_Controller{
function __construct(){
parent::__construct();
}
function index(){
$this->load->view('profile')
}
}
You will be able to see the contents inside of views/profile.php
To reduce confusion, it is essential that you read this before going forward.
That is the wrong way to do it, read the manual about how to setup your .htaccess file and how routing works. You don't need to have multiple index.php-like files, the should only be one.
Here is the link from where you should start reading.
Like the other said. Everything goes through index.php. But you can actually hide it so that you don't have to type it in the url, which is not pretty btw.
Go to your applications/config/config.php and find something like the following:
/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = "index.php";
remove the index.php. And in the .htaccess (which is next to your index.php) you need to add this If I'm not mistaken:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
If everything works you can call your profile controller like http://localhost/php/ci/profile_control. By default it calls the index action so make sure you have an public function index in that controller. The url structure is always http://url.com/*controller*/*action*/*extra params here*
If you wish to call another action (function) inside your controller, for example public function profile(), you can call this url http://localhost/php/ci/profile_control/profile.
It is in that second action that you specify your other view file with $this->load->view('profile'). That will call the view/profile.php file
You can also pass values to that action like this -> http://localhost/php/ci/profile_control/profile/id/7.
in your profile action you need to grab those values as follow
public function profile($action, $value)
{
//$action = the word id and $value = the number 7
}
You can create another .htaccess file inside the project folder but outside application folder with the following code
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I also had the same issue where only my index page was working and It worked for me!
I have a slight issue. I have configured my default controller as :
c$route['default_controller'] = 'news';
so when I point my browser to http://localhost/mysite all the news get loaded as instructed by the news controller.
Now on this page, I have given links to "read article" to read the full article. For this I have to point my browser to http://localhost/index.php/news/view/news-slug. I want to remove index.php from the url. I had already tried re-routing using wildcard without any success. Can you tell me how to do that?
There is a documentation on removing the index.php from the URL if you are using Apache with it's mod_rewrite:
Removing the index.php file
Using the following rewrite code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Also, there are many similar questions to yours, have a look at these questions from the search:
Search query on removing index.php in Codeigniter
You need to define your .htaccess file for this. Check this wiki: http://codeigniter.com/wiki/mod_rewrite
I am having a problem in implementing jquery.ajax in codeigniter. I want to send the control to a specific function of a controller. I am setting the url in my javascript function like this
var url='<?php echo('First/index');?>';
var ajaxoptions={url:url,success:submit_ajax_response};
First is my controller and index is my function where I want to send the control. When I click on the event on which it is called the following url is formed
http://localhost/codeigniter/First/index
The URL is fine but it is generating the error of 404. I have done such kind of operations various times in zendframework but unable to accomplish this job in codeigniter. I have noticed one thing that if I add index.php in the url it works fine. By adding index.php the url becomes like
http://localhost/codeigniter/index.php/First/index
I am astonished how to remove index.php from route file. I have only two lines in route.php file
$route['default_controller'] = "First";
$route['404_override'] = '';
I have already made my controller as the default controller.
Am I doing correct? What is the problem and how to accomplish this job`
You need to check a couple of things. First, in /application/config/config.php make sure index file is set to this:
$config['index_page'] = '';
Second, make sure you have correct .htaccess. This should be at the root of your public directory (same place as your index.php):
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Options FollowSymLinks
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
I notice the part that is missing form your URL is the "index.php" thing that CodeIgniter has.
Change your code to this: (You need URL helper, so load that before this):
var url="<?php echo (index_page() . 'First/index');?>";
var ajaxoptions={url:url,success:submit_ajax_response};
index_page returns your site "index" page, as specified in your config file.
In order to remove index.php from your CodeIgniter links see here.