Welcome Guest, Not a member yet? Register   Sign In
Call function from controller in view
#1

(This post was last modified: 11-19-2021, 02:05 AM by cvlancvlan.)

How can i call products() function from CategoryController in app/views/category.php

app/views/category.php
<?php $this->products() ?>

app/controllers/CategoryController.php
PHP Code:
<?php namespace App\Controllers;

class 
CategoryController extends BaseController {
 
 protected 
$category;
 
 public function 
__construct() {

 
parent::__construct();
 
 }
 
 public function 
initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger) {
 
 
parent::initController($request$response$logger);
 
 }
 
 public function 
index() {
 
 
$alias implode('/'$this->request->uri->getSegments());
 
 
$db db_connect();
 
$builder $db->table("category");
 
$builder->select("*");
 
$builder->where('alias'$alias);
 
$this->category $builder->get()->getRowArray();
 
 return 
view('category', ['category' => $this->category]);
 
 }
 
 protected function 
products() {
 
 if ( 
$this->category ) {
 
 
$db db_connect();
 
$builder $db->table("products");
 
$builder->select("*");
 
$builder->where('id_category', (int) $this->category['id_category']);
 return 
view('list/products', ['products' => $builder->get()->getRowArray()]);
 
 }
 
 
 }
 

Reply
#2

You should not be calling a method from a controller in you views, that would be breaking the MVC rule.

If you need something like that then create a library or helper with the method in it.

To execute a method in the controller use an CodeIgniter achor or html link.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB