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

Documentation out of date? discoverLocal() #581

Closed
karlgray opened this issue Jul 1, 2017 · 20 comments
Closed

Documentation out of date? discoverLocal() #581

karlgray opened this issue Jul 1, 2017 · 20 comments

Comments

@karlgray
Copy link

karlgray commented Jul 1, 2017

I am trying to get modules to work using the documentation here;
https://bcit-ci.github.io/CodeIgniter4/general/modules.html

In the Routes section It mentions,
$routes->discoverLocal(true);

This causes errors and I cannot find reference to this in the source.
Is the documentation out of date or am I being an idiot?

@daylightstudio
Copy link
Contributor

daylightstudio commented Jul 2, 2017

That config value will search for other Routes.php files under the namespace Config throughout your registered Autoload folders and load them. So for example, say you have a folder of "blog" outside of your /application folder that has a similar folder structure as your application folder and the /application/Config/Autoload.php file has a $psr4 mapping of:

'Blog' => ROOTPATH.'blog/',

Then, in that /blog folder you have a blog/Config/Routes.php file that has a namespace of "Config". This file will automatically be loaded by the RouterCollection class:
https://github.com/bcit-ci/CodeIgniter4/blob/develop/system/Router/RouteCollection.php#L378

What errors are you seeing?

@lonnieezell
Copy link
Member

Definitely let us know the error you're experiencing otherwise it's hard to help. Additionally - ensure you have the latest CI4 code as that feature was only added late last week. If your code is not up to date, you'll hit an error that the method doesn't exist.

@karlgray
Copy link
Author

karlgray commented Jul 4, 2017

Thanks,

I have just done a pull of the latest code to ensure that I was on the latest code.

When I add this line to my application/Config/Routes.php file.

$routes->discoverLocal(true);

I get a the "Whoops! We seem to have hit a snag. Please try again later..." Page.

Without this line I cannot get any controller to work outside of the application/Controllers/ directory.

What I have done so far is as follows;

  1. Created a directory called Blog on the same level as the application directory.
  2. Added a single line to my prs4 in Autoload.php so it now looks like this.
$psr4 = [
			'Config'                     => APPPATH.'Config',
			APP_NAMESPACE                => APPPATH,			    // For custom namespace
			'App'                        => APPPATH,			    // To ensure filters, etc still found,
			'Tests\Support'              => TESTPATH.'_support',    // So custom migrations can run during testing
			'Blog'                       => ROOTPATH.'Blog/',
		];
  1. Created 2 directories in Blog called Config and Controllers
  2. In Blog/Config/Routes.php I have added the following;
<?php 
namespace Config;
$routes->group('blog', ['namespace' => 'Blog\Controllers'], function($routes)
{
    $routes->get('/', 'Blog::index');
});
  1. In Blog/Controllers.php I have the following;
<?php 
namespace Blog;

class Blog extends \CodeIgniter\Controller
{
    public function index()
    {
        echo "Success!";
    }
}

I get a 404 error;

Up until now I have used CI3 with HMVC so am not accustomed to namespaces yet. I am probably doing something really really stupid. Thank you for your patience.

@lonnieezell
Copy link
Member

At a glance it looks correct. The only thing I can see as a possibility currently is that in Autoload.php config file, you don't need a trailing slash, so like this:

$psr4 = [
			'Config'                     => APPPATH.'Config',
			APP_NAMESPACE                => APPPATH,			    // For custom namespace
			'App'                        => APPPATH,			    // To ensure filters, etc still found,
			'Tests\Support'              => TESTPATH.'_support',    // So custom migrations can run during testing
			'Blog'                       => ROOTPATH.'Blog',
		];

I thought that got automatically cleaned up but it's possible that's causing an issue. Please give that a try and let me know.

@lonnieezell
Copy link
Member

@karlgray did that change work for you? Or did you find something else that worked?

@lonnieezell
Copy link
Member

Taking another look at this and your error is the namespace of your Blog controller. Should be:

<?php 
namespace Blog\Controllers;

Basically, namespaces (with the autoloader) tell which directories to traverse to find the class in.

@karlgray
Copy link
Author

karlgray commented Jul 18, 2017

I am sorry for delay in responding.

I have changed the controller to this;

<?php 
namespace Blog\Controllers;

class Blog extends \CodeIgniter\Controller
{
	public function index()
	{
		echo "Success!";
	}
}

I am still getting a 404 :(

@karlgray
Copy link
Author

karlgray commented Jul 18, 2017

I have totally cleared out the install and started from scratch. I done a fresh git clone.
I have created a new directory called Blog on the same level as the application directory.
I have then created 2 new directories called
/Blog/Controllers
/Blog/Config

In /Blog/Controllers/Blog.php I have

<?php 
namespace Blog\Controllers;

class Blog extends \CodeIgniter\Controller
{
	public function index()
	{
		echo "Success!";
	}
}

In /Blog/Config/Autoload.php I have update the $prs4 to this;

		$psr4 = [
			'Config'                     => APPPATH.'Config',
			APP_NAMESPACE                => APPPATH,			    // For custom namespace
			'App'                        => APPPATH,			    // To ensure filters, etc still found,
			'Tests\Support'              => TESTPATH.'_support',    // So custom migrations can run during testing
			'Blog'                       => ROOTPATH.'Blog',
		];

in /Blog/Config/Routes.php i have this;

<?php 

namespace Config;
$routes->group('blog', ['namespace' => 'Blog\Controllers'], function($routes)
{
	$routes->get('/', 'Blog::index');
});

I have made no other changes to any other files.

I must be doing some seriously dumb I guess :(

@lonnieezell
Copy link
Member

Biggest thing I see is that you forgot to tell CodeIgniter where the Blog namespace lives. You added the namespace to Blog/Config/Autoload instead of application/Config/Autoload. Unless you just mis-typed?

@karlgray
Copy link
Author

Thanks, Yes I had done this correctly but mis-quoted above. I have edited my previous post to reflect this.
/application/Config/Autoload.php
/Blog/Config/Routes.php

@karlgray
Copy link
Author

I am still concerned/Confused by the documentation.
On this page.

In the Routes section it says the following;

Routes
By default, routes are not automatically scanned for within modules. This is to boost performance when modules are not in use. However, it’s a simple thing to scan for any Routes file within modules. Simply change the discoverLocal setting to true in /application/Config/Routes.php:

$routes->discoverLocal(true);

However there is no mention of $routes->discoverLocal(true); in /application/Config/Routes.php
and when I put it in I get a Whoops error.

Has this call been changed/removed? Or is the manual out of date?

@lonnieezell
Copy link
Member

The docs are correct. And discoverLocal is in the develop branch of the routes file: https://github.com/bcit-ci/CodeIgniter4/blob/develop/application/Config/Routes.php#L67.

It's true there's nothing in that docblock. Heck, I'm thinking of removing most of that docblock because it takes up so much space. :)

If all you're seeing is a "Whoops" screen, you should set your environment to development so that you can see the errors that are popping up. That should help out. With any luck, it's simply a typo in your controller.

Also, I don't know if it's just the example, but it looks like you might have your routes above the declaration of $routes in the routes file? Though, I'm guessing you just stripped those out to show me.

And, finally - to check out a working example, take a look at my Simple Forums repo.

@lonnieezell lonnieezell reopened this Jul 18, 2017
@karlgray
Copy link
Author

I will look at your example. Thank you :)

I get the whoops screen when I add the line;
$routes->discoverLocal(true);
to /application/Config/Routes.php

On the $routes setting. I mistyped as well. I have updated my error there.
In my /Blog/Config/Routes.php
I have this and nothing else;

<?php 

namespace Config;
$routes->group('blog', ['namespace' => 'Blog\Controllers'], function($routes)
{
	$routes->get('/', 'Blog::index');
});

I have not changed anything in /application/Config/Routes.php

Apologies for any confusion on that. I will go look at your example now. Thanks.

@karlgray
Copy link
Author

Strange. I just went to re-add the $routes->discoverLocal(true); to my Routes.php file and saw it was already there but set to false. changed it to true and it all works.

I am seriously confused by why it wasn't there on my previous pull and didn't work when I added it manually and now does.

However... As it works now I am not going to worry myself about it.

Thank you very much for looking into this for me and putting up with my daft questions.

@lonnieezell
Copy link
Member

No worries! Glad you got it working.

@ghost
Copy link

ghost commented May 23, 2018

I think the documentation is not clear...
It saying to add /acme/Blog/Config/Routes.php for routing (or as I understand...), but core libraries are searching for "acme/Config/Routes.php" at every request on localhost/blog

Look at discoverRoutes() method, and their inside call to file locator search: $this->fileLocator->search('Config/Routes.php');

Inside search method, there are "if (file_exists($folder . $path))", where $folder is "/workspace/CodeIgniter4/acme/" and path "Config/Routes.php".

Is this the correct behaviour or there's something else to do?
My blog module example has this structure:

acme/
+-- Blog
|   +-- Controllers
|   |   L-- Blog.php
L-- Config
    L-- Routes.php

Putting "Config/Routes.php" inside acme directory has working.
I've tryied with latest development sources today.

@lonnieezell
Copy link
Member

It searches for the Config folder within all of the namespaces you've defined. If you only have a namespace defined for Acme, then it would only look for acme/Config/Routes, but if you have a namespace defined for Acme\Blog it would look for acme/Blog/Config/Routes.

Where do you think that needs to be made clearer?

@ghost
Copy link

ghost commented May 24, 2018

I read the documentation and find what you said.

But now I have another question, what is the purpose to have two routes configurations?
Isn't more simple to search only for the one in the module? Since I think you have already got the module name in the uri.

Sorry for my carelessness :)

@lonnieezell
Copy link
Member

Sorry, I don't understand the question. The module is Blog not Acme. There might be multiple modules in the same namespace of Acme. In the interest of performance, we do not scan every folder underneath a namespace's main folder for the file/folder we are looking for. The performance could be fairly drastic at that point.

Either way - this is not a bug. Please take other support questions to the forums :)

@ghost
Copy link

ghost commented May 25, 2018

Sorry, maybe my english is not good enough, I will discuss it on the forum if I need something else.

Thanks :)

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

No branches or pull requests

4 participants