• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing $this->request in view without having to pass it into view

Is there a way of accessing the incomingRequest class within a view without having to pass it in a data array.
I would like to do the same with the db class so I can call $this->db->query() directly in the view.

for instance I did the following initially:

public function location()
    $data = $this->data;

    $data['uri'] = $this->request->uri;

    return view('/path/to/view', $data);

But is there no nice way of accessing $this->request directly in the view without having to pass it into view

I want to do something like this:
    $colorClass = $this->request->uri->getSegment(1) == 'workflow' ? 'bg-warning' : '';

<nav class="<?= $colorClass ?>" >
<!-- rest of code here -->

without having to pass the request directly to the $data array.

What I have done as a work around is the following:


namespace App\Models;

use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\HTTP\RequestInterface;

class HelperFunctions
  protected $request;
  protected $db;

  public function __construct(ConnectionInterface &$db, RequestInterface $request) {
    $this->db = &$db;
    $this->request = $request;

  function getSegment($segment) {
    return $this->request->uri->getSegment($segment);

namespace App\Controllers;

use CodeIgniter\Controller;
use App\Models\HelperFunctions;
use TDAuth\Libraries\TDAuth;

class BaseController extends Controller
  protected $fn;
  protected $db;

  public function initController(\CodeIgniter\HTTP\RequestInterface $request) {
    $this->db = \Config\Database::connect();
    $this->fn = new HelperFunctions($this->db, $request);
so that on view:
    $colorClass = $fn->getSegment(1) == 'workflow' ? 'workflow' : '';

<nav class="<?= $colorClass ?>" ></nav>

This provides access to the request and db but I would need to create a method in helper functions class for each type of method I would want to call against the request class.

Many thanks in advance for suggestions, and or a better way of doing this.

PHP Code:
$db db_connect(); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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