Welcome Guest, Not a member yet? Register   Sign In
SOLVED: CI 4.0.2 and subdirectories
#1
Thumbs Up 
(This post was last modified: 04-12-2020, 02:13 PM by mariofix. Edit Reason: Solved problem )

Hi! I've just been playing with CI 4.0.2 and I noticed I can't use subdirectories in my Controllers folder, everything else works fine, I installed via composer.

I have $routes->setAutoRoute(true); in Routes.php
if i use this address http://localhost:8080/home works fine, but if create a directory and put the same Home.php file, I get a 404 error
Controllers/Home.php -> http://localhost:8080/home (Works)
Controllers/dot2/Home.php -> http://localhost:8080/dot2/home or http://localhost:8080/dot2/home/index (Doesn't Work)

I get this error
404 - File Not Found

Controller or its method is not found: App\Controllers\Dot2::Home


Have you encounter the same problem?

Thanks!!

-mariofix
Reply
#2

(This post was last modified: 04-04-2020, 02:38 AM by jreklund.)

I am facing the same problem for the Controllers and the Views too
Reply
#3

You need to namespace all folders and sub-folders.

If in the Controllers directory then:
PHP Code:
<?php
namespace App\Controllers

If the Controller is in a sub directory:
PHP Code:
<?php
namespace App\Controllers\sub-directory

When extending or using a Controller from a sub directory:
PHP Code:
use App\Controllers\sub-directory 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(03-13-2020, 08:14 AM)InsiteFX Wrote: You need to namespace all folders and sub-folders.

If in the Controllers directory then:
PHP Code:
<?php
namespace App\Controllers

If the Controller is in a sub directory:
PHP Code:
<?php
namespace App\Controllers\sub-directory

When extending or using a Controller from a sub directory:
PHP Code:
use App\Controllers\sub-directory 

Please, add it info in documentation:  https://codeigniter4.github.io/userguide...controller
Reply
#5

Same issue, i spend sometimes to figure out the right way...

Agree with @juancarlosv. please add it to the documentation.
I enjoy coding in codeigniter4. SurveyIgniter a project where i learn about codeigniter4.
Reply
#6

(03-13-2020, 08:14 AM)InsiteFX Wrote: You need to namespace all folders and sub-folders.

If in the Controllers directory then:
PHP Code:
<?php
namespace App\Controllers

If the Controller is in a sub directory:
PHP Code:
<?php
namespace App\Controllers\sub-directory

When extending or using a Controller from a sub directory:
PHP Code:
use App\Controllers\sub-directory 

Excellent! Thank you, it's working now.

a few things before:
- Yes, you need to add the folder name into the namespace
- the folder must start with an upper-case letter
- The folder has the same rules as any Controller file (I think)


PHP Code:
<?php namespace App\Controllers\Dot2;

use 
CodeIgniter\Controller;

class 
Test extends Controller 
    

Reply
#7

Thank you so much, I had same problem also, this solved my problem too, I checked documentation first but couldn't find the best explanation about it there.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB