CodeIgniter Forums
"Json" as controller name does not work - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: Issues (https://forum.codeigniter.com/forumdisplay.php?fid=19)
+--- Thread: "Json" as controller name does not work (/showthread.php?tid=74707)



"Json" as controller name does not work - HarrysR - 10-27-2019

Well i've faced a very weird issue.

If i create a controller named "Json" and try to access it i get an "Object not found" error in my CI project. On the other hand if i give the controller another name it'll work like a charm or if i use an Uppercase first letter it will also work.

Here's my controller, filename and routes files.

Json.php
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Json extends CI_Controller {
 public function 
index(){
   
// Some code
 
}


Routes.php
PHP Code:
$route['(?i)json']              'json';
$route['(?i)json/(:any)']       'json/$1'

Any ideas?


RE: "Json" as controller name does not work - InsiteFX - 10-29-2019

Strange I just ran it with CodeIgniter 3.1.11 with no errors without using routes.

Maybe something to do with your routing.


RE: "Json" as controller name does not work - HarrysR - 10-29-2019

(10-29-2019, 03:42 AM)InsiteFX Wrote: Strange I just ran it with CodeIgniter 3.1.11 with no errors without using routes.

Maybe something to do with your routing.

Well my full routing file is: 

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

$route['xml']                   'xml';
$route['xml/(:any)']            'xml/$1';

$route['json']                  'json';
$route['json/(:any)']           'json/$1';

$route['dashboard']             'members';
$route['account']               'members/account';
$route['login']                 'members/login';
$route['register']              'members/register';

$route['members']               'members';
$route['members/(:any)']        'members/$1';

$route['(:any)']                'pages/$1';

$route['default_controller']    'pages';

$route['404_override']          '';
$route['translate_uri_dashes']  FALSE

I' ll check if there's something wrong with the .htaccess file.


RE: "Json" as controller name does not work - dave friend - 10-29-2019

The following isn't very useful because it basically asks that you follow CI's normal controller/method/arg URI relationship

PHP Code:
$route['json/(:any)']  'json/$1' 

In other words, you're not actually remapping anything.

You really need to know which method you want to run and put that in the value. For instance, if you want it all to go to index().

PHP Code:
$route['json/(:any)']  'json/index/$1' 

Naturally, index() would need to accept arguments, i.e..

PHP Code:
class Json extends CI_Controller {
 public function 
index($arg null){
  // Some code
 
}


Notice that index has an optional argument. If you add that then you don't need this route.

PHP Code:
$route['json'] = 'json'