CodeIgniter 404 not found controller - php

I know the question has been asked many times but I really can't solve the issue.
My default controller is working, but if I want to access another controller I will have the 404 issue.
My controller file name has the upper case as Utilisateur.php,
So I'm trying to access the Controller function inscription by inscription
Here is my config:
$config['base_url'] = 'http://localhost:63342/WeBusy/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
Here is my rooting:
$route['default_controller'] = 'accueil';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
And finally the controler:
class Utilisateur extends CI_Controller {
public function inscription(){
$data = array('header' => 'HeaderAccueil.php', 'body' => 'utilisateur/AjoutUtilForm.php');
$this->load->view('StructureHtml.php', $data);
}
Thanks

Give complete path to your anchor
inscription
But the best practice is to give dynamic url like using url helper or constants.
For Example if use url helper function your anchor path is like.
inscription
Don't forget to load url helper in your autoload file or your class constructor.

I found the issues,
So I needed to create a .htaccess file in the root of the applicaiton in order to remove the index.php AND this still doesn't work with phpstorm, maybe there is something to configure but it is working smoothly on EasyPhp :)

Check your controller filename, should be Utilisateur.php (ucfirst).
Check if your mod_rewrite works:
http://localhost:63342/WeBusy/index.php?q=utilisateur/inscription
add new method index() {die('test');} to your Utilisateur controller and change $route['default_controller'] = 'accueil'; to: $route['default_controller'] = 'utilisateur';

Related

Routing folders to index

I am currently under CodeIgniter 3.1.13, trying to root every single request to my Core controller. I'm starting CodeIgniter, I'm missing something as it didn't work.
I'm searching to get the bold part below as $1, and always aim the same controller.
site.com/ <- Working
site.com/test <- Working
site.com/test/test2 <- This one isn't working.
Here is my routes configuration :
$route['default_controller'] = 'Core';
$route['(:any)'] = 'Core/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = true;
I though (:any) would also handle that case.
Is there any way to do this properly ?
The Core controller is in \codeigniter\application\controllers directory.
I wonder if using REGEX couldn't work here. To be honest, I want to send the full part as $1 :
site.com/home/test/example
class Core extends CI_Controller {
// Below $page would be equal to "test/test2"
public function view($page = "index") {
}
Any idea ?

Why do I have to include index in URL CodeIgniter?

I have created a controller call posts like below
<?php
class Posts extends CI_Controller {
public function index(){
$data['title'] = 'Latest posts';
$this->load->view('templates/header');
$this->load->view('posts/index', $data);
$this->load->view('templates/footer');
}
}
I have set routes like below
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'pages/view';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['(:any)'] = 'pages/view/$1';
$route['posts'] = 'posts/index';
but if I go to the url http://localhost/Blog/posts it dies with 404 page error
But if i go to http://localhost/Blog/posts/index it works fine
why I cant get it to work in the first url? what did I do wrong please help me with this i am new to codeigniter
Looks like Blog might be a subdirectory where you have your CodeIgniter installation.
CI will treat any incoming uri as /controller/action/parameters and try to instance the corresponding Controller class. If that's the case, it's trying to instance a controller named Blog and can't find it.
To fix this you have to update your config.php and set:
$config['base_url'] = 'http://' . $_SERVER['HTTP_HOST'] . '/Blog/';
change $route['posts'] to $route['/posts'] in router or remove that router since index method will get called as default.

Codeigniter route URL contact page

I'm new in Codeigniter I'm not sure how to use Codeigniter Routing. I've created the Contact.php in the controller folder and contact.php in the views folder.
In routes.php I have put $route['Contact'] = 'controller/contact'; but when I enter the url http://mytest.dev/contact/ it shows 404 Page Not Found. The page you requested was not found.
I want when I enter "http://mytest.dev/contact" it will show the contact page
Thanks in advance.
Controller
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Contact extends CI_Controller {
function __construct() {
parent::__construct();
}
function index() {
$this->load->view('contact')
}
}
In CI there is an index.php in the URL ( by default ). So, you can access your page with this url http://mytest.dev/index.php/contact
For removing it from URL and have it like you want you need to add .htaccess file in your project directory
Check this answer for it
Also, you don't need to change your routes.php every time after creating a new page. Leave it like this
$route['default_controller'] = 'welcome'; // or contact
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
I see you are using $route['Contact'] = 'controller/contact';
Tell us your controller's class name and which function/method did you use from the above you are referencing to contact() and that controller name doesn't make sense. You route will normally be in lowercase too.
If you named your class Contact (which seems like it) then you need to put a .htaccess file in the folder where your index.php or base_url resides (or root directory) and then remove the value in application/config.php as $config['index_page'] = ''; so that you can access it from http://mytest.dev/contact
To make it clearer. The format should be $route['AAA'] = 'BBB/CCC';
AAA is the url path of your choice
BBB is the name of your controller's class
CCC is the function/method of the page you want to show
If you didn't add the htaccess, you must put /index.php/ before your preferred path.

CodeIgniter : page not found issue appear when i call default controller in Php?

Structure of my controller directory:
--Controller
--frontend
--user_controller
When i call default controller it says 404 page not found
Route i have set :
$route['default_controller'] = "frontend/user_controller";
$route['admin_panel/site'] = "site";
$route['admin_panel'] = "backend/admin_controller/dashboard";
$route['admin_panel/login'] = "backend/admin_controller/index";
$route['admin_panel/(:any)'] = "backend/admin_controller/$1";
$route['admin_panel/(:any)/(.*)'] = "backend/admin_controller/$1/$2";
And when i run http://localhost/example/frontend/user_controller then it works.
So but didn't work this url : http://localhost/example
Please try with: move default controller at bottom
$route['admin_panel/site'] = "site";
$route['admin_panel'] = "backend/admin_controller/dashboard";
$route['admin_panel/login'] = "backend/admin_controller/index";
$route['admin_panel/(:any)'] = "backend/admin_controller/$1";
$route['admin_panel/(:any)/(.*)'] = "backend/admin_controller/$1/$2";
$route['default_controller'] = "frontend/user_controller";
Codeigniter will not allow default controller to be in a sub directory.
You have to rewrite it as
$route['default_controller'] = "user_controller";
And place that directly in the controller folder
You can refer this thread to avoid this. But I didn't tested it.
It is better to avoid the core functionalities as it may affect the updates.
I have not tested this but potentially you could use this to get around the sub-folder?
$route['frontend'] = 'frontend/user_controller';
$route['default_controller'] = 'frontend';
need to make sure there is a 'public function index()' in there though as you aren't specifying the method to load...

CodeIgniter routes: I keep getting a 404

I have a application/controller/login.php file containing the following
class Login extends Controller {
function Login()
{
parent::Controller();
}
function index()
{
$this->mysmarty->assign('title', 'Login');
$this->mysmarty->assign('site_media', $this->config->item('site_media'));
$this->mysmarty->display('smarty.tpl');
}
}
My routes definition looks like the following:
$route['default_controller'] = "welcome";
$route['login'] = 'login';
$route['scaffolding_trigger'] = "";
The problem is that I keep getting a 404 when I try to access http://localhost/myapp/login. What did I do wrong? I've checked CI routes docs and cannot spot anything.
There shouldn't be anything wrong with this - does it work without the route?
Also, have you set the .htaccess properly (i.e. does http://localhost/myapp/index.php/login work instead)
Another point to keep in mind, if you have have "enable_query_strings" set to true and aren't using the .htaccess mod_rewrite rules:
In config/config.php:
$config['enable_query_strings'] = TRUE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
$config['directory_trigger'] = 'd';
The URL to route your request properly would look like this:
http://localhost/myapp/index.php?c=login
If you have not removed your index.php from your app using the .htaccess file, well thats another ball game on its own. But what you can do is this:
Set a default controller that's not the welcome file and if you want the welcome file to be your default use the link this way
http://localhost/myapp/index.php/login
Remember this way only works when the index.php file has not been removed using the .htaccess file.
Also open the apllication/config/config.php and use this code for the baseurl
$config['base_url'] = 'http://localhost/myapp';
That should always do the trick.

Categories