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

Bug: #2397

Closed
haojielyb opened this issue Nov 13, 2019 · 11 comments
Closed

Bug: #2397

haojielyb opened this issue Nov 13, 2019 · 11 comments
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@haojielyb
Copy link

haojielyb commented Nov 13, 2019

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

@haojielyb haojielyb added the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 13, 2019
@dafriend
Copy link
Contributor

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.

@haojielyb
Copy link
Author

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);

}`

@dafriend
Copy link
Contributor

the $path I pass in not action

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

@haojielyb
Copy link
Author

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

@lonnieezell
Copy link
Member

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
Copy link
Contributor

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

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

@haojielyb
Copy link
Author

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
Copy link
Member

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

@lonnieezell lonnieezell reopened this Nov 15, 2019
@haojielyb
Copy link
Author

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
Copy link
Member

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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

3 participants