Welcome Guest, Not a member yet? Register   Sign In
Autoload Librari and Helper in codeigniter4
#5

I have this function in Common.php. So, it wraps the view in the main viwe, also checks if is any present in the helpers array and load depending on the helpers that I send in $data. Like this:

function renderView(string $view, array $data = [], array $options = []): mixed
{
$ionAuth = new \IonAuth\Libraries\IonAuth();
$Images = new \App\Models\Images();

extract($data);

$avatar = $Images->where('type','user')->where('external_id',$ionAuth->user()->row()->id)->orderBy('main','desc')->find();

$avatar = ($avatar!==[]) ? imageUrl('user',$avatar[0]['name']) : null;

if(!empty($helpers))
{
foreach($helpers as $helper)
{
helper($helper);
}
}

return view('dashboard',[
'title' => esc($title),
'subtitle' => isset($subtitle) ? esc($subtitle) : null,
'bodycontent' => view($view,$data),
'user' => esc($ionAuth->user()->row()),
'isadmin' => esc($ionAuth->isAdmin()),
'active' => isset($active) ? esc($active) : null,
'activecontroller' => isset($activecontroller) ? esc($activecontroller) : null,
'breadcrumbs' => esc($breadcrumbs),
'avatar' => $avatar,
]);
}

And I call it like:

return renderView('purchases/purchaseedit', [
'title' => lang('purchase.core.title'),
'bodycontent' => lang('purchase.core.contacts'),
'boxtitle' => lang('purchase.core.create'),
'boxfooter' => sprintf(lang('providers.core.lines'), $this->pageLen),
'active' => 'purchase',
'activecontroller' => 'purchase',

'details' => $details,
'purchase' => $purchase,
'provider' => $provider,
'products' => $this->Providers->getArticlesOfProvider($provider['id']),

'paginate' => $this->Purchases->paginate($this->pageLen),
'count' => $this->Purchases->countFindAll(),
'pagelen' => $this->pageLen,
'pager' => $this->Providers->pager,
'Time' => new Time(),
'locale' => $this->request->getLocale(),
'provArts' => $this->Providers,
'errors' => $errors ?? null,
'helpers' => ['form'],

'breadcrumbs' => [
[
'url' => base_url(),
'text' => lang('app.dashboard'),
'icon' => lang('app.icons.dashboard'),
'active' => false,
],
[
'url' => base_url('purchases'),
'text' => lang('purchase.core.title'),
'icon' => lang('purchase.core.icon'),
'active' => false,
],
[
'url' => '',
'text' => lang('purchase.core.create'),
'icon' => lang('purchase.core.icon-plus'),
'active' => true,
]
]
]);
Reply


Messages In This Thread
RE: Autoload Librari and Helper in codeigniter4 - by PetrusPetrorum - 05-06-2021, 04:10 AM



Theme © iAndrew 2016 - Forum software by © MyBB