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:
Code:
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:
Code:
<?php
$colorClass = $this->request->uri->getSegment(1) == 'workflow' ? 'bg-warning' : '';
?>
<nav class="<?= $colorClass ?>" >
<!-- rest of code here -->
</nav>
without having to pass the request directly to the $data array.
What I have done as a work around is the following:
Code:
<?php
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);
}
}
and:
Code:
<?php
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) {
parent::initController($request);
$this->db = \Config\Database::connect();
$this->fn = new HelperFunctions($this->db, $request);
}
}
so that on view:
Code:
<?php
$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.