Skip to content

Commit

Permalink
Merge pull request #1402 from jim-parry/docs/namespace
Browse files Browse the repository at this point in the history
Correct class namespacing in the user guide
  • Loading branch information
jim-parry authored Nov 3, 2018
2 parents 862bc8d + c6af3c0 commit 90365de
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 45 deletions.
6 changes: 4 additions & 2 deletions user_guide_src/source/cli/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ Let's create a simple controller so you can see it in action. Using your
text editor, create a file called Tools.php, and put the following code
in it::

<?php
class Tools extends \CodeIgniter\Controller {
namespace App\Controller;
use CodeIgniter\Controller;

class Tools extends Controller {

public function message($to = 'World')
{
Expand Down
32 changes: 21 additions & 11 deletions user_guide_src/source/extending/core_classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ For example, if you have a new ``App\Libraries\RouteCollection`` class that you
the core system class, you would create your class like this::

namespace App\Libraries;
use CodeIgniter\Router\RouteCollectionInterface;

class RouteCollection implements \CodeIgniter\Router\RouteCollectionInterface
class RouteCollection implements RouteCollectionInterface
{

}
Expand Down Expand Up @@ -78,28 +79,37 @@ identical to replacing a class with a one exception:

For example, to extend the native RouteCollection class, you would declare your class with::

class RouteCollection extends \CodeIgniter\Router\RouteCollection
namespace App\Libraries;
use CodeIgniter\Router\RouteCollection;

class RouteCollection extends RouteCollection
{

}

If you need to use a constructor in your class make sure you extend the parent constructor::

class RouteCollection implements \CodeIgniter\Router\RouteCollection
{
public function __construct()
{
parent::__construct();
}
}
namespace App\Libraries;
use CodeIgniter\Router\RouteCollection;

class RouteCollection extends RouteCollection
{
public function __construct()
{
parent::__construct();
}
}

**Tip:** Any functions in your class that are named identically to the methods in the parent class will be used
instead of the native ones (this is known as “method overriding”). This allows you to substantially alter the CodeIgniter core.

If you are extending the Controller core class, then be sure to extend your new class in your application controller’s
constructors::

class Home extends App\BaseController {
namespace App\Controllers;
use App\BaseController;

}
class Home extends BaseController {

}

8 changes: 5 additions & 3 deletions user_guide_src/source/general/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ If you need to create a new configuration file you would create a new file at yo
**/application/Config** by default. Then create the class and fill it with public properties that
represent your settings::

<?php namespace Config;
namespace Config;
use CodeIgniter\Config\BaseConfig;

class App extends \CodeIgniter\Config\BaseConfig
class App extends BaseConfig
{
public $siteName = 'My Great Site';
public $siteEmail = '[email protected]';
Expand Down Expand Up @@ -199,8 +200,9 @@ the same way as described for namespaced variables.
A sample configuration class setup for this::

namespace App\Config;
use CodeIgniter\Config\BaseConfig;

class MySalesConfig extends \CodeIgniter\Config\BaseConfig
class MySalesConfig extends BaseConfig
{
public $target = 100;
public $campaign = "Winter Wonderland";
Expand Down
39 changes: 27 additions & 12 deletions user_guide_src/source/incoming/controllers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ Let's try it: Hello World!
Let's create a simple controller so you can see it in action. Using your text editor, create a file called Blog.php,
and put the following code in it::

<?php
class Blog extends \CodeIgniter\Controller
{
namespace App\Controllers;
use CodeIgniter\Controller;

class Blog extends Controller
{
public function index()
{
echo 'Hello World!';
Expand All @@ -53,15 +55,19 @@ If you did it right, you should see::

This is valid::

<?php
class Blog extends \CodeIgniter\Controller {
namespace App\Controllers;
use CodeIgniter\Controller;

class Blog extends Controller {

}

This is **not** valid::

<?php
class blog extends \CodeIgniter\Controller {
namespace App\Controllers;
use CodeIgniter\Controller;

class blog extends Controller {

}

Expand All @@ -82,8 +88,11 @@ controller gets called.**

Let's try it. Add a new method to your controller::

<?php
class Blog extends \CodeIgniter\Controller {
namespace App\Controllers;
use CodeIgniter\Controller;

class Blog extends Controller
{

public function index()
{
Expand Down Expand Up @@ -114,8 +123,11 @@ For example, let's say you have a URI like this::

Your method will be passed URI segments 3 and 4 ("sandals" and "123")::

<?php
class Products extends \CodeIgniter\Controller {
namespace App\Controllers;
use CodeIgniter\Controller;

class Products extends Controller
{

public function shoes($sandals, $id)
{
Expand Down Expand Up @@ -286,7 +298,10 @@ You can define an array of helper files as a class property. Whenever the contro
these helper files will be automatically loaded into memory so that you can use their methods anywhere
inside the controller::

class MyController extends \CodeIgniter\Controller
namespace App\Controllers;
use CodeIgniter\Controller;

class MyController extends Controller
{
protected $helpers = ['url', 'form'];
}
Expand Down
5 changes: 4 additions & 1 deletion user_guide_src/source/incoming/incomingrequest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ Accessing the Request
An instance of the request class already populated for you if the current class is a descendant of
``CodeIgniter\Controller`` and can be accessed as a class property::

class UserController extends CodeIgniter\Controller
namespace App\Controllers;
user CodeIgniter\Controller;

class UserController extends Controller
{
public function index()
{
Expand Down
16 changes: 12 additions & 4 deletions user_guide_src/source/models/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ Creating Your Model
To take advantage of CodeIgniter's model, you would simply create a new model class
that extends ``CodeIgniter\Model``::

class UserModel extends \CodeIgniter\Model
use CodeIgniter\Model;

class UserModel extends Model
{

}
Expand All @@ -64,7 +66,9 @@ This ensures that within the model any references to ``$this->db`` are made thro
connection.
::

class UserModel extends \CodeIgniter\Model
use CodeIgniter\Model;

class UserModel extends Model
{
protected $DBGroup = 'group_name';
}
Expand All @@ -79,7 +83,9 @@ The model class has a few configuration options that can be set to allow the cla
to work seamlessly for you. The first two are used by all of the CRUD methods to determine
what table to use and how we can find the required records::

class UserModel extends \CodeIgniter\Model
use CodeIgniter\Model;

class UserModel extends Model
{
protected $table = 'users';
protected $primaryKey = 'id';
Expand Down Expand Up @@ -348,7 +354,9 @@ simplest, they might look like this::

A very simple model to work with this might look like::

class JobModel extends \CodeIgniter\Model
use CodeIgniter\Model;

class JobModel extends Model
{
protected $table = 'jobs';
protected $returnType = '\App\Entities\Job';
Expand Down
6 changes: 5 additions & 1 deletion user_guide_src/source/testing/controllers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ The Helper Trait
You can use either of the base test classes, but you do need to use the ``ControllerTester`` trait
within your tests::

namespace CodeIgniter;

use Tests\Support\Helpers\ControllerTester;

class TestControllerA extends \CIDatabaseTestCase
Expand All @@ -28,9 +30,11 @@ the request body, URI, and more. You specify the controller to use with the ``co
fully qualified class name of your controller. Finally, call the ``execute()`` method with the name of the method
to run as the parameter::

namespace CodeIgniter;

use Tests\Support\Helpers\ControllerTester;

class TestControllerA extends CIDatabaseTestCase
class TestControllerA extends \CIDatabaseTestCase
{
use ControllerTester;

Expand Down
6 changes: 4 additions & 2 deletions user_guide_src/source/tutorial/create_news_items.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ fields in the ``$allowedFields`` property.

::

<?php
class NewsModel extends \CodeIgniter\Model
namespace App\Models;
use CodeIgniter\Model;

class NewsModel extends Model
{
protected $table = 'news';

Expand Down
12 changes: 5 additions & 7 deletions user_guide_src/source/tutorial/news_section.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ your database properly as described :doc:`here <../database/configuration>`.

::

<?php

namespace App\Models;
use CodeIgniter\Model;

class NewsModel extends \CodeIgniter\Model
class NewsModel extends Model
{
protected $table = 'news';
}
Expand Down Expand Up @@ -99,11 +97,11 @@ a new ``News`` controller is defined. Create the new controller at

::

<?php namespace App\Controllers;

namespace App\Controllers;
use App\Models\NewsModel;
use CodeIgniter\Controller;

class News extends \CodeIgniter\Controller
class News extends Controller
{
public function index()
{
Expand Down
6 changes: 4 additions & 2 deletions user_guide_src/source/tutorial/static_pages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ code.

::

<?php namespace App\Controllers;
class Pages extends CodeIgniter\Controller {
namespace App\Controllers;
use CodeIgniter\Controller;

class Pages extends Controller {

public function view($page = 'home')
{
Expand Down

0 comments on commit 90365de

Please sign in to comment.