Codeigniter 3 - Make URLs case insensitive - php

With CI3 released controllers must now be Ucword-style (for whatever reason). No problem changing these, but upgrading any site now leads to 404s wherever it's applicable (which is pretty much everywhere).
Is there a way to make it so the old URLs still work (in addition)? Ie I have a controller 'Admin.php" the index() fn of which used to be called
http://example.com/admin
now it must be called
http://example.com/Admin
Is there a way to have both work (on CentOS). Maybe via Apache rewrite and/or config?

You can set your routes in :
application/config/routes.php
$route['admin'] = 'admin/index';
You can set your Controller as following code in controller.
application/controllers/Admin.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Admin extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model(array('admin_model'));
}
public function index()
{
$this->template->view('admin/index');
}
}
?>

If case insensitive routes are required, do below changes to URI.php
Location of File: system/core/URI.php
Find $this->_parse_request_uri() and replace it with strtolower($this->_parse_request_uri())

Related

Called controller can't access anything

in codeigniter I have my main controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller
{
public function index()
{
$this->load->library('../controllers/forum');
$obj = new $this->forum();
$obj->test();
}
}
And the controller I'm trying to access:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Forum extends CI_Controller
{
function __construct()
{
echo "testing1";
$this->load->library('session');
parent::__construct();
$this->load->database();
$this->load->model('model_forum');
}
public function index(){
}
public function test(){
echo "testing2";
$this->data['forums'] = $this->model_forum->getForums();
$this->load->view('homepage', $this->data);
}
}
Everything is fine with my model_forum.php file, because it works if I put all the code in Main controller. But if I'm trying to access Forum controller, nothing works, only "testing1" echo goes through. Picture of error:
Anyone has any idea what I'm doing wrong? I'm new to PHP and codeigniter so I'm struggling a little bit. Thanks in advance.
You can't load a controller from a controller in CI - unless you use HMVC or something.
You should think about your architecture a bit. If you need to call a controller method from another controller, then you should probably abstract that code out to a helper or library and call it from both controllers.
UPDATE
After reading your question again, I realize that your end goal is not necessarily HMVC, but URI manipulation. Correct me if I'm wrong, but it seems like you're trying to accomplish URLs with the first section being the method name and leave out the controller name altogether.
If this is the case, you'd get a cleaner solution by getting creative with your routes.
For a really basic example, say you have two controllers, controller1 and controller2. Controller1 has a method method_1 - and controller2 has a method method_2.
You can set up routes like this:
$route['method_1'] = "controller1/method_1";
$route['method_2'] = "controller2/method_2";
Then, you can call method 1 with a URL like http://example.com/method_1 and method 2 with http://example.com/method_2.
Albeit, this is a hard-coded, very basic, example - but it could get you to where you need to be if all you need to do is remove the controller from the URL.
You could also go with remapping your controllers.
From the docs: "If your controller contains a function named _remap(), it will always get called regardless of what your URI contains.":
public function _remap($method)
{
if ($method == 'some_method')
{
$this->$method();
}
else
{
$this->default_method();
}
}

Route undefined for my controller CodeIgniter

I'm on CodeIgniter and I have a problem.
I created a Controller called View.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class View extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('client');
}
public function index(){
echo "Test";
/*$client = $this->client->getClientByID($idClient);
echo json_encode($client);*/
}
}
But when I call him in the browser, he says : The requested URL /Modules/CommExpert/View/ was not found on this server.
index() in the Controller View is defined and just has an echo in it.
Can someone explains me ? Thanks :D
Part I.
If you get stuck - perform a test by doing something a little different.
For instance - Create a new module called test
Under your new modules/test/controllers/ create a new controller called test.php or for CI 3 Test.php
So now you have /application/modules/test/controllers/Test.php ( for CI 3 )
In your test.php controller put
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index(){
echo "This is the Test controller from my test module";
}
}
And see if you can get that to work!
Part II:
I see that you have tried /CommExpert/View in your URL and got a 404.
Be careful of case. Windows and MAC's don't care about case sensitivity. Linux Systems do.
Did you try yourdomain.com/commexpert/view.
After a little experimenting I found that you can have a controller called view (personally, I'd try to go for something different).
Also you can have either view or View in the URL and both work.
What is important is the case of the controller.
In my testing where I have a module called admin, and I have created a controller called view, I get the following... **
NOTE: This is for CI 3.0
URL Result
domain.com/admin/view -> Works ( Lower case v in view )
domain.com/admin/View -> Works ( Upper case V in View )
domain.com/Admin/view -> 404 ( Big Admin, Lower case v in view )
domain.com/Admin/View -> 404 ( Big Admin, Lower case V in view )
See if that helps you any!
Here's my .htaccess :
Options -Indexes
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|uploads|editor|css|js|scripts|images|img|media|xml|user_guide|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Codeigniter URI route

I have various static pages and I dont want to create independent controller for them all.
Say I want to create a controller named page and have all of the static pages as functions.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Page extends Frontend_Controller {
public function index()
{
}
public function store(){
$this->load->view('public/store');
}
public function contact(){
$this->load->view('public/contact');
}
public function about(){
$this->load->view('public/about');
}
}
Is there anyway to modify the URI routes so that I wouldnt have to type mydomain.com/page/contact but I could type mydomain.com/contact to view the page
In your application/config/routes.php
You can add as:
$route['contact'] = "page/contacts";
$route['store'] = "page/store";
...
In my application/config/routes.php I use the following regex to do that. Most sites I work on are relatively small so I list all of the controllers in the following snippet (because it's easier than listing all of the static pages)
$route['^(?!controller1|controller2).*'] = "page/$1";
The logic basically says that If the path does not start with controller1 or controller2 then use page as the controller for urls like http://domain.tld/page
For not having to create 10+ static routes for all my static pages it keeps it pretty simple.

Codeigniter: session class in routes.php

How can I access the codeigniter session class inside routes.php?
I need that class to route all the request (except /login) to a certain controller, unless the user has admin privileges ($this->session->userdata('logged')).
All the route rules are working, I only need to access that class.
You are not typically able to access the Singleton ($this->) from the config & routes file because the classes are not loaded at that point
although there are a few workarounds to access the session, the better way would be to a MY_Controller & the _remap() function:
http://ellislab.com/codeigniter/user-guide/general/controllers.html#remapping
Here's some sample code that explains a little more about how they work:
http://www.codebyjeff.com/blog/2012/11/ci-_remap-function-the-friend-you-never-knew-you-had
Create this MY_Controllerand store it in application/core/, then have your other controllers extend it:
<?php if (! defined('BASEPATH')) exit('No direct script access');
class MY_Controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->_check_auth();
}
private function _check_auth(){
if(!$this->session->userdata('is_admin')){
$this->session->sess_destroy();
redirect('login');
}
}
}
Note: above code assumes you already have a user login system in place.

Can't access a Codeigniter controller in sub folder

I have set up a folder with a controller in it: controllers/admin/home.php, but I get a 404 from the browser when I try to access it.
This is my routes file:
$route['employers'] = "employers/home";
//$route['employers/dash'] = "employers/dash";
$route['default_controller'] = "home";
$route['404_override'] = '';
This is the controller file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class home extends CI_Controller {
function __construct(){
parent::__construct();
/*
enable profiler
*/
//$this->output->enable_profiler(TRUE);
$this->load->helper('url');
$this->load->library('ion_auth');
$this->load->library('session');
$this->load->library('form_validation');
$this->load->helper('layout');
}
}
.htaccess seems fine standard. Any ideas on what i'm doing wrong?
Note some things:
1) Routes are executed in the order they're written, and your custom routes MUST follow the default ones. So, it should be:
$route['default_controller'] = "home";
$route['404_override'] = '';
$route['employers'] = "employers/home";
This if your controller "home" is inside the folder "employers".
2) Controllers don't need all that stuff you wrote, indeed you don't even need to call the parent constructor unless you're planning to load libraries and resource for the whole controller's methods (which can be achieve also by autoloading them in the autoload.php file), so it could simply be:
file: application/controllers/employers/home.php
class Home extends CI_Controller {
function index()
{
// this is the method you're calling with your URL!
}
}
3) As per above, and as already pointed out by #Wesley, with your url you're trying to access the INDEX method of your controller HOME in your subfolder EMPLOYERS. But you didn't defined an index() method (which is the one called by default if no other is supplied).
It seems, instead, that CI is trying to look for an employers controller and a home method; if it doesnt find it, but you have a employers folder, it tries to access the index method in the home controller in the employers folder. And, since it didn't find it either, you're getting the 404 page.
Hope I'm clear, otherwise just ask.
You failed to say how you were trying to access it via url. It should be:
{YOUR_BASE_URL}admin/home
... followed by optional URL segments (/method/param1/param2/etc).
Without the additional segments, this would by default load the index method. However, since you don't have any methods defined, there's nothing to load.
If this still fails after you define a method, move the controller file out of the sub-directory for starters, and make sure it works.

Categories