|
| 1 | +# Installation |
| 2 | + |
| 3 | +These instructions assume that you have already [installed the CodeIgniter 4 app starter](https://codeigniter.com/user_guide/installation/installing_composer.html) as the basis for your new project, set up your **.env** file, and created a database that you can access via the Spark CLI script. |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +- [Composer](https://getcomposer.org) |
| 8 | +- Codeigniter **v4.2.7** or later |
| 9 | +- A created database that you can access via the Spark CLI script |
| 10 | + - InnoDB (not MyISAM) is required if MySQL is used. |
| 11 | + |
| 12 | +## Composer Installation |
| 13 | + |
| 14 | +Installation is done through [Composer](https://getcomposer.org). The example assumes you have it installed globally. |
| 15 | +If you have it installed as a phar, or otherwise you will need to adjust the way you call composer itself. |
| 16 | + |
| 17 | +```console |
| 18 | +composer require codeigniter4/shield |
| 19 | +``` |
| 20 | + |
| 21 | +### Troubleshooting |
| 22 | + |
| 23 | +#### IMPORTANT: composer error |
| 24 | + |
| 25 | +If you get the following error: |
| 26 | + |
| 27 | +```console |
| 28 | +Could not find a version of package codeigniter4/shield matching your minimum-stability (stable). |
| 29 | +Require it with an explicit version constraint allowing its desired stability. |
| 30 | +``` |
| 31 | + |
| 32 | +1. Run the following commands to change your [minimum-stability](https://getcomposer.org/doc/articles/versions.md#minimum-stability) in your project `composer.json`: |
| 33 | + |
| 34 | + ```console |
| 35 | + composer config minimum-stability dev |
| 36 | + composer config prefer-stable true |
| 37 | + ``` |
| 38 | + |
| 39 | +2. Or specify an explicit version: |
| 40 | + |
| 41 | + ```console |
| 42 | + composer require codeigniter4/shield:dev-develop |
| 43 | + ``` |
| 44 | + |
| 45 | + The above specifies `develop` branch. |
| 46 | + See <https://getcomposer.org/doc/articles/versions.md#branches> |
| 47 | + |
| 48 | + ```console |
| 49 | + composer require codeigniter4/shield:^1.0.0-beta |
| 50 | + ``` |
| 51 | + |
| 52 | + The above specifies `v1.0.0-beta` or later and before `v2.0.0`. |
| 53 | + See <https://getcomposer.org/doc/articles/versions.md#caret-version-range-> |
| 54 | + |
| 55 | +## Initial Setup |
| 56 | + |
| 57 | +### Command Setup |
| 58 | + |
| 59 | +1. Run the following command. This command handles steps 1-5 of *Manual Setup* and runs the migrations. |
| 60 | + |
| 61 | + ```console |
| 62 | + php spark shield:setup |
| 63 | + ``` |
| 64 | + |
| 65 | + > **Note** |
| 66 | + > If you want to customize table names, you must change the table names |
| 67 | + > before running database migrations. |
| 68 | + > See [Customizing Table Names](../customization/table_names.md). |
| 69 | + |
| 70 | +2. Configure **app/Config/Email.php** to allow Shield to send emails with the [Email Class](https://codeigniter.com/user_guide/libraries/email.html). |
| 71 | + |
| 72 | + ```php |
| 73 | + <?php |
| 74 | + |
| 75 | + namespace Config; |
| 76 | + |
| 77 | + use CodeIgniter\Config\BaseConfig; |
| 78 | + |
| 79 | + class Email extends BaseConfig |
| 80 | + { |
| 81 | + /** |
| 82 | + * @var string |
| 83 | + */ |
| 84 | + public $fromEmail = 'your_mail@example.com'; |
| 85 | + |
| 86 | + /** |
| 87 | + * @var string |
| 88 | + */ |
| 89 | + public $fromName = 'your name'; |
| 90 | + |
| 91 | + // ... |
| 92 | + } |
| 93 | + ``` |
| 94 | + |
| 95 | +### Manual Setup |
| 96 | + |
| 97 | +There are a few setup items to do before you can start using Shield in |
| 98 | +your project. |
| 99 | + |
| 100 | +1. Copy the **Auth.php**, **AuthGroups.php**, and **AuthToken.php** from **vendor/codeigniter4/shield/src/Config/** into your project's config folder and update the namespace to `Config`. You will also need to have these classes extend the original classes. See the example below. These files contain all the settings, group, and permission information for your application and will need to be modified to meet the needs of your site. |
| 101 | + |
| 102 | + ```php |
| 103 | + // new file - app/Config/Auth.php |
| 104 | + <?php |
| 105 | + |
| 106 | + declare(strict_types=1); |
| 107 | + |
| 108 | + namespace Config; |
| 109 | + |
| 110 | + // ... |
| 111 | + use CodeIgniter\Shield\Config\Auth as ShieldAuth; |
| 112 | + |
| 113 | + class Auth extends ShieldAuth |
| 114 | + { |
| 115 | + // ... |
| 116 | + } |
| 117 | + ``` |
| 118 | + |
| 119 | +2. **Helper Setup** The `setting` helper needs to be included in almost every page. The simplest way to do this is to add it to the `BaseController::initController()` method: |
| 120 | + |
| 121 | + ```php |
| 122 | + public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) |
| 123 | + { |
| 124 | + $this->helpers = array_merge($this->helpers, ['setting']); |
| 125 | + |
| 126 | + // Do Not Edit This Line |
| 127 | + parent::initController($request, $response, $logger); |
| 128 | + } |
| 129 | + ``` |
| 130 | + |
| 131 | + This requires that all of your controllers extend the `BaseController`, but that's a good practice anyway. |
| 132 | + |
| 133 | +3. **Routes Setup** The default auth routes can be setup with a single call in **app/Config/Routes.php**: |
| 134 | + |
| 135 | + ```php |
| 136 | + service('auth')->routes($routes); |
| 137 | + ``` |
| 138 | + |
| 139 | +4. **Security Setup** Set `Config\Security::$csrfProtection` to `'session'` (or set `security.csrfProtection = session` in your **.env** file) for security reasons, if you use Session Authenticator. |
| 140 | + |
| 141 | +5. **Migration** Run the migrations. |
| 142 | + |
| 143 | + > **Note** |
| 144 | + > If you want to customize table names, you must change the table names |
| 145 | + > before running database migrations. |
| 146 | + > See [Customizing Table Names](../customization/table_names.md). |
| 147 | + |
| 148 | + ```console |
| 149 | + php spark migrate --all |
| 150 | + ``` |
| 151 | + |
| 152 | + #### Note: migration error |
| 153 | + |
| 154 | + When you run `spark migrate --all`, if you get `Class "SQLite3" not found` error: |
| 155 | + |
| 156 | + 1. Remove sample migration files in **tests/_support/Database/Migrations/** |
| 157 | + 2. Or install `sqlite3` php extension |
| 158 | + |
| 159 | +6. Configure **app/Config/Email.php** to allow Shield to send emails. |
| 160 | + |
| 161 | + ```php |
| 162 | + <?php |
| 163 | + |
| 164 | + namespace Config; |
| 165 | + |
| 166 | + use CodeIgniter\Config\BaseConfig; |
| 167 | + |
| 168 | + class Email extends BaseConfig |
| 169 | + { |
| 170 | + /** |
| 171 | + * @var string |
| 172 | + */ |
| 173 | + public $fromEmail = 'your_mail@example.com'; |
| 174 | + |
| 175 | + /** |
| 176 | + * @var string |
| 177 | + */ |
| 178 | + public $fromName = 'your name'; |
| 179 | + |
| 180 | + // ... |
| 181 | + } |
| 182 | + ``` |
0 commit comments