Skip to content

Commit b2deed2

Browse files
authored
Merge pull request #1847 from MGatner/basecontroller
Extend Controller to BaseController by default
2 parents 73a5cd6 + bef3174 commit b2deed2

5 files changed

Lines changed: 111 additions & 14 deletions

File tree

app/Controllers/BaseController.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php namespace App\Controllers;
2+
3+
/**
4+
* Class BaseController
5+
*
6+
* BaseController provides a convenient place for loading components
7+
* and performing functions that are needed by all your controllers.
8+
* Extend this class in any new controllers:
9+
* class Home extends BaseController
10+
*
11+
* For security be sure to declare any new methods as protected or private.
12+
*
13+
* @package CodeIgniter
14+
*/
15+
16+
use CodeIgniter\Controller;
17+
18+
class BaseController extends Controller
19+
{
20+
21+
/**
22+
* An array of helpers to be loaded automatically upon
23+
* class instantiation. These helpers will be available
24+
* to all other controllers that extend BaseController.
25+
*
26+
* @var array
27+
*/
28+
protected $helpers = [ ];
29+
30+
/**
31+
* Constructor.
32+
*
33+
*/
34+
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
35+
{
36+
// Do Not Edit This Line
37+
parent::initController($request, $response, $logger);
38+
39+
//--------------------------------------------------------------------
40+
// Preload any models, libraries, etc, here.
41+
//--------------------------------------------------------------------
42+
// E.g.:
43+
// $this->session = \Config\Services::session();
44+
45+
}
46+
}

app/Controllers/Home.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use CodeIgniter\Controller;
44

5-
class Home extends Controller
5+
class Home extends BaseController
66
{
77
public function index()
88
{

system/Config/Routes.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,16 @@
4444
* It is called by Config\Routes, and has the $routes RouteCollection
4545
* already loaded up and ready for us to use.
4646
*/
47+
// Prevent access to BaseController
48+
$routes->add('basecontroller(:any)', function()
49+
{
50+
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
51+
});
52+
4753
// Migrations
4854
$routes->cli('migrations/(:segment)/(:segment)', '\CodeIgniter\Commands\MigrationsCommand::$1/$2');
4955
$routes->cli('migrations/(:segment)', '\CodeIgniter\Commands\MigrationsCommand::$1');
5056
$routes->cli('migrations', '\CodeIgniter\Commands\MigrationsCommand::index');
5157

52-
// CLI Catchall - uses a _remap to
58+
// CLI Catchall - uses a _remap to call Commands
5359
$routes->cli('ci(:any)', '\CodeIgniter\CLI\CommandRunner::index/$1');
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
************************
2+
Extending the Controller
3+
************************
4+
5+
CodeIgniter's core Controller should not be changed, but a default class extension is provided for you at
6+
**app/Controllers/BaseController.php**. Any new controllers you make should extend ``BaseController`` to take
7+
advantage of preloaded components and any additional functionality you provide::
8+
9+
<?php namespace App\Controllers;
10+
11+
use CodeIgniter\Controller;
12+
13+
class Home extends BaseController {
14+
15+
}
16+
17+
Preloading Components
18+
=====================
19+
20+
The base controller is a great place to load any helpers, models, libraries, services, etc. that you intend to
21+
use every time your project runs. Helpers should be added to the pre-defined ``$helpers`` array. For example, if
22+
you want the HTML and Text helpers universally available::
23+
24+
protected $helpers = ['html', 'text'];
25+
26+
Any other components to load or data to process should be added to the constructor ``initController()``. For
27+
example, if your project uses the Session Library heavily you may want to initiate it here::
28+
29+
public function initController(...)
30+
{
31+
// Do Not Edit This Line
32+
parent::initController($request, $response, $logger);
33+
34+
$this->session = \Config\Services::session();
35+
}
36+
37+
Additional Methods
38+
==================
39+
40+
The base controller is not routable (system config routes it to 404 Page Not Found). As an added security
41+
measure **all** new methods you create should be declared as ``protected`` or ``private`` and only be accessed through the
42+
controllers you create that extend ``BaseController``.
43+
44+
Other Options
45+
=============
46+
47+
You may find that you need more than one base controller. You can create new base controllers as long as any other controllers that you make extend the correct base. For example, if your project
48+
has an involved public interface and a simple administrative portal you may want to extend ``BaseController`` to
49+
the public controllers and make ``AdminController`` for any administrative controllers.
50+
51+
If you do not want to use the base controller you may bypass it by having your controllers extend the system
52+
Controller instead::
53+
54+
class Home extends Controller
55+
{
56+
57+
}

user_guide_src/source/extending/core_classes.rst

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,3 @@ If you need to use a constructor in your class make sure you extend the parent c
105105

106106
**Tip:** Any functions in your class that are named identically to the methods in the parent class will be used
107107
instead of the native ones (this is known as “method overriding”). This allows you to substantially alter the CodeIgniter core.
108-
109-
If you are extending the Controller core class, then be sure to extend your new class in your application controller’s
110-
constructors::
111-
112-
<?php namespace App\Controllers;
113-
114-
use App\BaseController;
115-
116-
class Home extends BaseController {
117-
118-
}
119-

0 commit comments

Comments
 (0)