Closed haojielyb closed 4 years ago
You have added a view()` method in _app/Common.php_ to override
view()` in system/Common.php? If I got that right, can you please share that code? Thanks.
ok this is the code `function view(string $name, array $data = [], array $options = []): string { /**
@var CodeIgniter\View\View $renderer */ $path = FCPATH . "views/ohthink"; $data['othv'] = base_url('views/ohthink'); $renderer = Services::renderer($path,null,false);
$saveData = null; if (array_key_exists('saveData', $options) && $options['saveData'] === true) { $saveData = (bool) $options['saveData']; unset($options['saveData']); }
return $renderer->setData($data, 'raw') ->render($name, $options, $saveData); }`
the $path I pass in not action
I apologize, but I do not understand what you are saying.
$path Cannot afford to work
the $path can not change viewPath in \CodeIgniter\View\View;
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.
@lonnieezell, Look again. haojielyb's call yours are the same.
$renderer = Services::renderer($path, null, false);
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
What does it do in production that it doesn't do in development? The overloaded method still runs, right? Need more details...
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
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.
@lonnieezell, Caused by problem fixed in PR #2381?
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