Welcome Guest, Not a member yet? Register   Sign In
Custom view method
#1

In CI3, I created a custom view method (cView) by extending CI_Loader :


Quote:// My controller, log user data and search queries
class MY_Loader extends CI_Loader
{
    public function __construct()
    {
        parent::__construct();
    }
   
    // Custom view loader, load and return the view if display is true
    public function cView($view, $vars = array(), $display = TRUE)
    {
        if (!$display) return '';
        return $this->view($view, $vars, TRUE);
    }
}



Since CI_Loader and the core folder no longer exist, I can't figure out where I should create my custom cView function in CI4? The main constrain is that cView() calls the native view() method.
Reply
#2

(This post was last modified: 05-06-2020, 11:33 AM by jreklund.)

Here are the complete code for view. You can create your own cView and put in in app/Common.php based on that. Or overwrite the system function.

https://codeigniter.com/user_guide/outgo...derer.html

PHP Code:
if (! function_exists('view'))
{
    
/**
     * Grabs the current RendererInterface-compatible class
     * and tells it to render the specified view. Simply provides
     * a convenience method that can be used in Controllers,
     * libraries, and routed closures.
     *
     * NOTE: Does not provide any escaping of the data, so that must
     * all be handled manually by the developer.
     *
     * @param string $name
     * @param array  $data
     * @param array  $options Unused - reserved for third-party extensions.
     *
     * @return string
     */
    
function view(string $name, array $data = [], array $options = []): string
    
{
        
/**
         * @var CodeIgniter\View\View $renderer
         */
        
$renderer Services::renderer();

        
$saveData config(View::class)->saveData;

        if (
array_key_exists('saveData'$options))
        {
            
$saveData = (bool) $options['saveData'];
            unset(
$options['saveData']);
        }

        return 
$renderer->setData($data'raw')
                        ->
render($name$options$saveData);
    } 
Reply
#3

(05-06-2020, 11:33 AM)jreklund Wrote: Here are the complete code for view. You can create your own cView and put in in app/Common.php based on that. Or overwrite the system function.

Thank for the answer, if I want to overwrite the system function, where should I put the code ?
Reply
#4

In app/Common.php, as it's loaded first.
Reply
#5

Thank you to all of you for these answers. I finally place my custom view in the BaseController since I use it everywhere in my apps: https://forum.codeigniter.com/thread-75176.html
Reply




Theme © iAndrew 2016 - Forum software by © MyBB