codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.3k stars 1.89k forks source link

Bug: #2397

Closed haojielyb closed 4 years ago

haojielyb commented 4 years ago

I am using CI4 rc3 when I edit .env CI_ENVIRONMENT = development
the function view in App/Common.php always get default \CodeIgniter\View\View $renderer = Services::renderer($path); the $path I pass in not action
if CI_ENVIRONMENT = production it canwork well
I think It because the debugger tool

dafriend commented 4 years ago

You have added a view()` method in _app/Common.php_ to overrideview()` in system/Common.php? If I got that right, can you please share that code? Thanks.

haojielyb commented 4 years ago

ok this is the code `function view(string $name, array $data = [], array $options = []): string { /**

dafriend commented 4 years ago

the $path I pass in not action

I apologize, but I do not understand what you are saying.

haojielyb commented 4 years ago

$path Cannot afford to work
the $path can not change viewPath in \CodeIgniter\View\View;

lonnieezell commented 4 years ago

Some part of the system is already creating an instance of the View class. The way you are calling it will always return a singleton, so you need to make sure to tell it not to return a sharedInstance but to create a new one:

$renderer = Services::renderer($path, null, false);

This will return a new instance that uses that path so you can then do with what you will. This is how I created a template engine on top of the view layer in Myth:Forums.

dafriend commented 4 years ago

@lonnieezell, Look again. haojielyb's call yours are the same.

$renderer = Services::renderer($path, null, false);
haojielyb commented 4 years ago

Services::renderer($path, null, false);
use this code can resolve this question
but when CI_ENVIRONMENT =production It's not going to happen
so I think it is bug in development ENVIRONMENT

lonnieezell commented 4 years ago

What does it do in production that it doesn't do in development? The overloaded method still runs, right? Need more details...

haojielyb commented 4 years ago

In the development environment app\common.php views do not overwrite views of the system because Some part of the system(may be is debug tool ) is already creating an instance of the View class. but in production environment app\common.php views work well because the debug tool dot not work

lonnieezell commented 4 years ago

This sounds like what I described above. And I'm not sure there's much to do about this, honestly. I've added a view() method similar to yours in my forum setup and the method gets called so I don't think I can call it a bug.

You're better served overriding the renderer service itself, or saving that instance to a base controller, like I've done in the link I gave above.

If you really want to continue with an overridden view() method, you might be able to grab an instance of the renderer, modify the path, and the inject that instance back into the services container. That sounds like more trouble than it's worth, though.

dafriend commented 4 years ago

@lonnieezell, Caused by problem fixed in PR #2381?