Download the PHP package marcogmonteiro/ci-admin-controller without Composer
On this page you can find all versions of the php package marcogmonteiro/ci-admin-controller. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download marcogmonteiro/ci-admin-controller
More information about marcogmonteiro/ci-admin-controller
Files in marcogmonteiro/ci-admin-controller
Package ci-admin-controller
Short Description View autoloading for Codeigniter 4 Framework
License MIT
Informations about the package ci-admin-controller
codeigniter 4 base controller
codeigniter-base-controller is an extended BaseController
class to use in your CodeIgniter applications. Any controllers that inherit from BaseController
or AdminController
get intelligent view autoloading and layout support. It's strongly driven by the ideals of convention over configuration, favouring simplicity and consistency over configuration and complexity.
Synopsis a controller that extends to adminController
</div>
<p>Footer</p>
If you wish to disable the layout entirely and only display the view - a technique especially useful for AJAX requests - you can set $this->layout
to FALSE
.
$this->layout = FALSE;
Like with $this->view
, $this->layout
can also be used to specify an unconventional layout file:
$this->layout = 'layouts/mobile.php';
Any variables set in $this->data
will be passed through to both the view and the layout files.
View structure
Your views should be created to support the built in functionality of layouts that comes with codeigniter 4
<h1>Hello World from the home/index view!</h1>
As for your layouts, those should have a render section called yield.
<!doctype html>
<html>
<head>
<title>My Layout</title>
</head>
<body>
This is my layout content
</body>
</html>
To actually be able to rendere a view directly without the layout we need an empty layout doing the render. For that there's a nolayout.php file included in your Views/layouts folder that only does the view render.
The complete folder structure is now included in the project.
Loading Helpers in your controllers
If you want to load helpers in your controllers in a global scope and not inside a function all your have to do is declare the helpers property as array with all your helpers, like so:
<?php namespace App\Controllers;
class Home extends AdminController
{
protected $helpers = ['url'];
public function index()
{
}
}
AdminController CRUD
There's a few functions that you can use in your adminController that are not avaiable in your baseController. To do that some rules must be followed. First you need to load the main model your controller will be working with. Let's say you have a Articles controller and a ArticleModel first you load your model.
$this->article = model('App\Models\ArticleModel');
As you can see I'm loading the model into a class property called article. In this case my _$modelclass property should also be called article.
$this->model_class = 'article';
This way if your Article controller needs access to a update functionallity all you need to do is create a update function like so:
public function update($id)
{
return $this->adminUpdate($id, $this->request->getPost());
}
This update function should always return the update result that was set on your adminController function.
In case you want to send the admin_update method something else other than your post data you can do it just like this:
public function update($id)
{
$data = $this->request->getPost();
$data['my_new_field'] = 'foobar';
return $this->adminUpdate($id, $this->request->getPost());
}
This is specially usefull if you want to add some extra data that was not given by the post.
By default the success action of this function will always redirect to your index function in your controller. Using this structure always assume that you have a index function.
This redirect will also set a confirm flashdata automatically that can be used in your views.
In case you need to override this behavior that can be done by returning a diferent result.
public function update($id)
{
$this->adminUpdate($id, $this->request->getPost());
return [
'url' => '/admin/list_articles',
'success' => 'Your article was updated.'
];
}
This way you're redirecting the user to your /admin/list_articles with the flashdata "your article was updated.".
Use AdminController in root directory
In case you want to use AdminController in root directory, or any other directory change the $directory property.
protected $directory = ''; // Set default directory
Add your own language variables
The CRUD methods now support the use of language variables. Those should be se in your app/languages folder depending on your locales settings. For now the following languages are supported:
- English
- Portuguese
Error Helper
In your adminController.php there's a custom helper being loaded called error helper. That should be placed in your helpers folder and it's only purpose is to serve as a shortcut for the 404 exception.
// This is just the same thing
show_404();
// as
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
You can now use this on your controllers too everytime you need to show a 404 error. Since its autoloaded on your adminController.
Roadmap
- Add better error messages based the class name, so we can say something like "Your article was update" instead of item;
Codeigniter 3 version, no CRUD controller, only autoload views
If you're still using codeigniter 3 and want something like this that can be found here: jamierumbelow/codeigniter-base-controller