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

Modules Controllers sub-directory problem #322

Closed
byazrail opened this issue Dec 8, 2016 · 15 comments
Closed

Modules Controllers sub-directory problem #322

byazrail opened this issue Dec 8, 2016 · 15 comments

Comments

@byazrail
Copy link
Contributor

byazrail commented Dec 8, 2016

Hi,

test types;
Acme\Controllers\Test\Foo.php;
access localhost/ci4/acme/test/foo, localhost/test/foo;

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

non sub-directory method running.
Acme\Controllers\Test - in foo() method;
access localhost/ci4/test/foo;

AutoRoute problem.
custom add routes running, test route;
$routes->add('sample', 'Acme\Controllers\Test\Foo::sample'); // only sample method.

@lonnieezell
Copy link
Member

What namespace did you use in your controller?

@byazrail
Copy link
Contributor Author

Acme Namespace;
Controllers Test\Foo
Function index

$routes->setDefaultNamespace('Acme\Controllers');
$routes->setAutoRoute(true);

localhost/ci4/test/foo - 404
localhost/ci4/acme/test/foo - 404

Modules in controllers sub-directory auto route problem.
Non sub directory no problem.

@lonnieezell
Copy link
Member

For file Acme\Controllers\Test\Foo.php you need to have a namespace of Acme\Controllers\Test.

@byazrail
Copy link
Contributor Author

Anything wrong?
screen shot 2016-12-29 at 09 27 53
screen shot 2016-12-29 at 09 28 18
screen shot 2016-12-29 at 09 28 30

@byazrail
Copy link
Contributor Author

Current Site\Controllers\Auth\Auth Controller;
auth/auth or auth not working folder and controller same name Auth\Auth.

@lonnieezell lonnieezell reopened this Jan 2, 2017
@lonnieezell
Copy link
Member

It looks like you've got things setup correctly. I'll have to dig into the code and see what's going on.

@lonnieezell
Copy link
Member

I take that back. Auto-Routing currently only works in APPATH.Controllers, which is why it's not working for you. However, it definitely makes sense the way you're trying to use it. I'll work on that.

@lonnieezell
Copy link
Member

And I must be rusty. :) While it doesn't look like it should work, because the full namespace is set on line 465 of Router.php, it all works as expected. Here's the steps I did so that you can reproduce:

  1. Create a new namespace by editing application/Config/Autoload.php:
$psr4 = [
			'Config'                     => APPPATH.'Config',
			APP_NAMESPACE                => APPPATH,			// For custom namespace
			'App'                        => APPPATH,			// To ensure filters, etc still found
			'Myth'                       => ROOTPATH.'_myth',
		];
  1. Change the default namespace for Controllers in application/Config/Routes.php:
$routes->setDefaultNamespace('Myth\Controllers');
  1. Create a new directory to hold the namespaced controllers at /_myth/Controllers.
  2. Create a new controller:
<?php namespace Myth\Controllers;

use CodeIgniter\Controller;

class Foo extends Controller {

    protected $helpers = ['url'];

    public function bar()
    {
        d(current_url());

        d('You found me!');
    }

}

If I try to access /foo or /foo/bar the expected page shows up. If I rename the controller to Home and the method to index, then the page shows up under the main site as it should.

Looking back at your files, your second set of files had a different namespace, etc, but you did not provide your routing settings, so you might have missed something there.

@byazrail
Copy link
Contributor Author

byazrail commented Jan 2, 2017

Hello,

setAutoRoute(true); // not working module sub-directory.
Own solution;

$routes->add('/', 'Site\Controllers\Site::index');

$routes->group('auth', function($routes)
{
	$routes->group('services', function($routes)
	{
		$routes->add('/', 'Site\Controllers\Auth\Services');
		$routes->add('(:any)', 'Site\Controllers\Auth\Services::$1');
	});
	$routes->group('game', function($routes)
	{
		$routes->add('/', 'Site\Controllers\Auth\Game');
		$routes->add('(:any)', 'Site\Controllers\Auth\Game::$1');
	});
	$routes->add('/', 'Site\Controllers\Auth\Auth');
	$routes->add('(:any)', 'Site\Controllers\Auth\Auth::$1');
});

@lonnieezell
Copy link
Member

Sorry you couldn't get the Auto-Route working, but the more explicit method there will definitely work for you. I was unable to recreate a broken system so there's nothing I can do at this time.

@BondaCB
Copy link
Contributor

BondaCB commented Jan 4, 2017

@lonnieezell

Hello, i have followed your steps in your previous post and I have some issues getting response for anything other than the home

Once I add other routes to the same controller and method, i get 404'd
My routes.php looks as follows:

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


$routes->add('/', 'Home::index');
$routes->add('/foo', 'Home::index');
$routes->add('/bar', 'Myth\Controllers\Home::index');

The / path works as expected, but neither /foo or /bar is found.
Why might this be?

Thanks

@lonnieezell
Copy link
Member

I can't look until later tonight, but typically you would not put the leading slash on a segment, so you might try like this:

$routes->add('/', 'Home::index');
$routes->add('foo', 'Home::index');
$routes->add('bar', 'Myth\Controllers\Home::index');

This is not really relevant to this issue, though, since this issue was about the Auto-Routing feature.

@BondaCB
Copy link
Contributor

BondaCB commented Jan 4, 2017

@lonnieezell It was that. I'm very sorry to have bothered you with such a simple question. But after several hours tinkering that detail escaped me.

Thank you very much

@lonnieezell
Copy link
Member

No worries. And that's something I'll need to look at since it should still work there. Thanks.

@ghulamhyder
Copy link

you make folder out of controller add base controller path through namespace could and go to route change default route type this

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

4 participants