Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get Controllers subfolder working #3347

Closed
mridah opened this issue Jul 18, 2020 · 4 comments
Closed

Can't get Controllers subfolder working #3347

mridah opened this issue Jul 18, 2020 · 4 comments

Comments

@mridah
Copy link

mridah commented Jul 18, 2020

I've gone through the following links among others but can't get it working. Maybe I'm missing something.

https://forum.codeigniter.com/thread-65608.html

#322

https://stackoverflow.com/questions/61133250/how-to-work-with-subdirectory-controllers-in-codeigniter-4

This is my directory structure for Controllers ( BaseController.php is the default one):

app
|----Controllers
      |----BaseController.php
      |----Home.php
      |----Users
            |----Login.php
            |----Signup.php
            |----Index.php

This is my Login.php :

<?php namespace App\Controllers\Users;

use CodeIgniter\Controller;
use App\Models\UsersModel;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;

class Login extends Controller
{
    protected $users_model;
    protected $helpers = ['url', 'form'];

    public function index()
    {
           $this->users_model = new UsersModel();
           echo 'In login';
    }
}

This is my Routes.php:

$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);

$routes->get('/', 'Home::index');

$routes->add('users/login', 'App\Controllers\Users\Login::index');

But I'm getting a 404 when I go to the url http://localhost/project_path/users/login

How can I fix this ? @lonnieezell @hex-ci @zahhar @kenjis

@michalsn
Copy link
Member

The default namespace is already set to App\Controllers so you should not add this namespace to your route definition.

$routes->add('users/login', 'Users\Login::index');

Also, please ask support questions in the Forum.

@mridah
Copy link
Author

mridah commented Jul 18, 2020

@michalsn thanks a lot !!! Was stuck on this for 3 days.
Can you please help me out with one more thing?

Suppose I want a route /users , what should be the approach. Should I create a file index inside users directory and within the file create a class named index or something else?

@michalsn
Copy link
Member

Yes, I think you can do that. But if you really want to talk about your app's design choices, start a forum topic. There are lots of helpful developers to help you. Here we only deal with bugs. Thanks.

@mridah
Copy link
Author

mridah commented Jul 18, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants