404 with CodeIgniter on form submit - php

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)

Related

Put language index into URL Multilanguage CodeIgniter site

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';

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

new controller is not working in Codeigniter

I'm using CodeIgniter. I set my default controller is 'cart'. And i add one more file in Controller folder that name is admin.php. admin.php having following code,
<?php
class Admin extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
echo 'Hello World!';
}
}
?>
If i give the url like http://lo...host/codeigniter/admin means, It shows 404 Not Found. If i give http://lo...host/codeigniter/, It shows cart page as good.
And if i change default controller in routes.php as 'admin', http://lo...host/codeigniter/ shows 'Hello World!'
Seem like your are not removing index.php from your URLs. Try to access to this URL:
http://lo...host/codeigniter/index.php/admin
If it works, then add this to your .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
More info:
http://ellislab.com/codeigniter/user-guide/general/urls.html

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.

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