• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CI4 libaries function call in VIEW

#1
Hello Everyone,

I am using CI 4.0.4
XAMPP CONTROL 3.2.4

I AM USING CI 4.0.4 FOR MY ONGOING LIVE PROJECT

CURRENTLY I AM FACING SOME ISSUE

I HAVE CREATED LIBRARIES


Code:
<?php
namespace App\Libraries;
use CodeIgniter\HTTP\RequestInterface;

class Product
{
  protected $session;
  protected $obj;
  private $module;

  function __construct($one,$two) {
    helper(['html', 'url', 'form']);
    $this->obj = $this;
    $this->session = \Config\Services::session();
    $this->session->start();
    $this->module = $this->session->get('modulevalue');
  }

  function Check_operation($final,$operation)
  {
    if(isset($this->obj->module[$final][$operation]))
      return 1;
    else
      return 0;
  }

}

in basecontroller.php

Code:
protected $libcon;
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        $this->libcon= new Product($one,$two);

    }


in products.php controller


Code:
<?php namespace App\Controllers;

class Products extends BaseController
{
    public function recentPosts()
    {
        $data = [];
        echo view('templates/header', $data);
        echo view('products');  // here in above products view file i need to access libraries Check_operation , and this i have to used in multiple view files
        echo view('templates/footer');
    }

}

How i can i access libraries Check_operation in above products view file

view file of products.php

i taught of using

Code:
<?= view_cell('\App\Libraries\Product::Check_operation', ['4' , '7']) ?>

but it giving me ArgumentCountError 0


in ci3

we can use directly in view

like

Code:
<?php $this->libcon->Check_operation(4,7) ;?>

but ci4 - cant

please let me know
Reply

#2
PHP Code:
<?= view_cell('\App\Libraries\Product::recentPosts', ['4' '7']) ?>

You have no "recentPosts" function in you libraries. Adjust this line to call the right function.

Also, unless it's to render a block of html to display in your view, you shouldn't call a library directly from your view.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply

#3
(10-26-2020, 02:28 PM)includebeer Wrote:
PHP Code:
<?= view_cell('\App\Libraries\Product::recentPosts', ['4' '7']) ?>

You have no "recentPosts" function in you libraries. Adjust this line to call the right function.

Also, unless it's to render a block of html to display in your view, you shouldn't call a library directly from your view.


sorry sir , my bad
by mistakenly putted that : have updated properly


PHP Code:
<?= view_cell('\App\Libraries\Product::Check_operation', ['4' '7']) ?>

 still not getting

don't want to render --  just need to call above method  , then how to do .
as its getting difficult for me, to understand

thanks
Reply

#4
In this example, they pass an array because the function expect an array as the only parameter. 

For a list of parameters, I think you need to use the second example and specify the name of the parameters in a string. 
Try this:

PHP Code:
<?= view_cell('\App\Libraries\Product::Check_operation''final=4, operation=7']) ?>
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply

#5
(10-27-2020, 04:25 AM)includebeer Wrote: In this example, they pass an array because the function expect an array as the only parameter. 

For a list of parameters, I think you need to use the second example and specify the name of the parameters in a string. 
Try this:

PHP Code:
<?= view_cell('\App\Libraries\Product::Check_operation''final=4, operation=7']) ?>

getting below message


ArgumentCountError

Too few arguments to function App\Libraries\Product::__construct(), 0 passed in H:\xampp749\htdocs\aks\system\View\Cell.php on line 123 and exactly 2 expected
Reply

#6
I have an error in my example, remove the ] at the end:

PHP Code:
<?= view_cell('\App\Libraries\Product::Check_operation''final=4, operation=7'?>
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply

#7
(10-27-2020, 02:47 PM)includebeer Wrote: I have an error in my example, remove the ] at the end:

PHP Code:
<?= view_cell('\App\Libraries\Product::Check_operation''final=4, operation=7'?>

Sorry sir,
my bad again

but still

getting below message


ArgumentCountError

Too few arguments to function App\Libraries\Product::__construct(), 0 passed in H:\xampp749\htdocs\aks\system\View\Cell.php on line 123 and exactly 2 expected

after reading  

Quote:The only restriction is that the class can not have any constructor parameters. This is intended to be used within views, and is a great aid to modularizing your code.

is that what error ???
Reply

#8
(10-27-2020, 11:01 PM)shashi Wrote:
Quote:The only restriction is that the class can not have any constructor parameters. This is intended to be used within views, and is a great aid to modularizing your code.

is that what error ???

Yes you found it! I didn’t see it at first, but the error message is pretty clear. The system instantiate your class with no parameter (because it wouldn’t know what to pass anyway), but your constructor expect 2 parameters:

Quote:Too few arguments to function App\Libraries\Product::__construct(), 0 passed in H:\xampp749\htdocs\aks\system\View\Cell.php on line 123 and exactly 2 expected
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply

#9
He has his constructor setup to receive two values but he is passing in an array from the view.
he needs to change his constructor to an array or pass in another value in his view.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#10
(10-29-2020, 03:27 AM)InsiteFX Wrote: He has his constructor setup to receive two values but he is passing in an array from the view.
he needs to change his constructor to an array or pass in another value in his view.

No he cannot change his constructor to an array, he needs to change his constructor to have no parameters. This is the code that is crashing in system/View/Cell.php at line 123

PHP Code:
$instance = new $class(); 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.