Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into 4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Nov 1, 2023
2 parents 3da89ac + 2df4a3f commit bfa6137
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 21 deletions.
4 changes: 2 additions & 2 deletions system/Commands/Generators/CellGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CellGenerator extends BaseCommand
*
* @var string
*/
protected $description = 'Generates a new Cell file and its view.';
protected $description = 'Generates a new Controlled Cell file and its view.';

/**
* The Command's Usage
Expand All @@ -55,7 +55,7 @@ class CellGenerator extends BaseCommand
* @var array<string, string>
*/
protected $arguments = [
'name' => 'The cell class name.',
'name' => 'The Controlled Cell class name.',
];

/**
Expand Down
2 changes: 1 addition & 1 deletion system/Debug/Toolbar/Views/toolbar.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</style>

<script id="toolbar_js">
var ciSiteURL = "<?= site_url() ?>"
var ciSiteURL = "<?= rtrim(site_url(), '/') ?>"
<?= file_get_contents(__DIR__ . '/toolbar.js') ?>
</script>
<div id="debug-icon" class="debug-bar-ndisplay">
Expand Down
4 changes: 4 additions & 0 deletions system/HotReloader/HotReloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ final class HotReloader
{
public function run(): void
{
if (session_status() === PHP_SESSION_ACTIVE) {
session_write_close();
}

ini_set('zlib.output_compression', 'Off');

header('Cache-Control: no-store');
Expand Down
15 changes: 11 additions & 4 deletions user_guide_src/source/incoming/filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,12 @@ You should define as many aliases as you need.
$globals
--------

The second section allows you to define any filters that should be applied to every request made by the framework.
The second section allows you to define any filters that should be applied to every valid request made by the framework.

You should take care with how many you use here, since it could have performance implications to have too many
run on every request. Filters can be specified by adding their alias to either the before or after array:
run on every request.

Filters can be specified by adding their alias to either the ``before`` or ``after`` array:

.. literalinclude:: filters/005.php

Expand All @@ -130,14 +133,18 @@ Except for a Few URIs

There are times where you want to apply a filter to almost every request, but have a few that should be left alone.
One common example is if you need to exclude a few URI's from the CSRF protection filter to allow requests from
third-party websites to hit one or two specific URI's, while keeping the rest of them protected. To do this, add
third-party websites to hit one or two specific URI's, while keeping the rest of them protected.

To do this, add
an array with the ``except`` key and a URI path (relative to BaseURL) to match as the value alongside the alias:

.. literalinclude:: filters/006.php

Any place you can use a URI path (relative to BaseURL) in the filter settings, you can use a regular expression or, like in this example, use
an asterisk (``*``) for a wildcard that will match all characters after that. In this example, any URI path starting with ``api/``
would be exempted from CSRF protection, but the site's forms would all be protected. If you need to specify multiple
would be exempted from CSRF protection, but the site's forms would all be protected.

If you need to specify multiple
URI paths, you can use an array of URI path patterns:

.. literalinclude:: filters/007.php
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/incoming/filters/004.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Filters extends BaseConfig
{
public array $aliases = [
'apiPrep' => [
'api-prep' => [
\App\Filters\Negotiate::class,
\App\Filters\ApiAuth::class,
],
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/incoming/filters/008.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Filters extends BaseConfig
// ...

public array $methods = [
'post' => ['InvalidChars', 'csrf'],
'post' => ['invalidchars', 'csrf'],
'get' => ['csrf'],
];

Expand Down
32 changes: 24 additions & 8 deletions user_guide_src/source/outgoing/view_cells.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ View Cells

Many applications have small view fragments that can be repeated from page to page, or in different places on the pages. These are often help boxes, navigation controls, ads, login forms, etc. CodeIgniter lets you encapsulate the logic for these presentation blocks within View Cells. They are basically mini-views that can be included in other views. They can have logic built in to handle any cell-specific display logic. They can be used to make your views more readable and maintainable by separating the logic for each cell into its own class.

CodeIgniter supports two types of View Cells: simple and controlled. Simple View Cells can be generated from any class and method of your choice and does not have to follow any rules, except that it must return a string. Controlled View Cells must be generated from a class that extends ``Codeigniter\View\Cells\Cell`` class which provides additional capability making your View Cells more flexible and faster to use.

.. contents::
:local:
:depth: 2

***************************
Simple and Controlled Cells
***************************

CodeIgniter supports two types of View Cells: simple and controlled.

**Simple View Cells** can be generated from any class and method of your choice and does not have to follow any rules, except that it must return a string.

**Controlled View Cells** must be generated from a class that extends ``Codeigniter\View\Cells\Cell`` class which provides additional capability making your View Cells more flexible and faster to use.

.. _app-cells:

*******************
Expand All @@ -18,11 +26,13 @@ Calling a View Cell

No matter which type of View Cell you are using, you can call it from any view by using the ``view_cell()`` helper function.

The first parameter is the name of the class and method to call, and the second parameter is an array of parameters to pass to the method:
The first parameter is (1) *the name of the class and method* (Simple Cell) or (2) *the name of the class and optional method* (Controlled Cell) to call,
and the second parameter is an array or string of parameters to pass to the method:

.. literalinclude:: view_cells/001.php

The Cell method must return a string, which will be inserted into the view where the ``view_cell()`` function was called.
The string that the Cell returns will be inserted into the view where the
``view_cell()`` function was called.

Namespace Omission
==================
Expand Down Expand Up @@ -67,8 +77,10 @@ Controlled Cells

.. versionadded:: 4.3.0

Controlled cells have two primary goals: to make it as fast as possible to build the cell, and provide additional logic and
flexibility to your views, if they need it. The class must extend ``CodeIgniter\View\Cells\Cell``. They should have a view file
Controlled cells have two primary goals: (1) to make it as fast as possible to build the cell, and (2) provide additional logic and
flexibility to your views, if they need it.

The class must extend ``CodeIgniter\View\Cells\Cell``. They should have a view file
in the same folder. By convention, the class name should be in PascalCase suffixed with ``Cell`` and the view should be
the snake_cased version of the class name, without the suffix. For example, if you have a ``MyCell`` class, the view file
should be ``my.php``.
Expand All @@ -79,7 +91,9 @@ should be ``my.php``.
Creating a Controlled Cell
==========================

At the most basic level, all you need to implement within the class are public properties. These properties will be made available to the view file automatically. Implementing the AlertMessage from above as a Controlled Cell would look like this:
At the most basic level, all you need to implement within the class are public properties. These properties will be made available to the view file automatically.

Implementing the AlertMessage from above as a Controlled Cell would look like this:

.. literalinclude:: view_cells/008.php

Expand Down Expand Up @@ -108,7 +122,9 @@ You can specify a custom view name by setting the ``view`` property in the class
Customize the Rendering
=======================

If you need more control over the rendering of the HTML, you can implement a ``render()`` method. This method allows you to perform additional logic and pass extra data the view, if needed. The ``render()`` method must return a string. To take advantage of the full features of controlled Cells, you should use ``$this->view()`` instead of the normal ``view()`` helper function:
If you need more control over the rendering of the HTML, you can implement a ``render()`` method. This method allows you to perform additional logic and pass extra data the view, if needed. The ``render()`` method must return a string.

To take advantage of the full features of controlled Cells, you should use ``$this->view()`` instead of the normal ``view()`` helper function:

.. literalinclude:: view_cells/012.php

Expand Down
5 changes: 5 additions & 0 deletions user_guide_src/source/outgoing/view_cells/001.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
// In a View.

// Simple Cell
<?= view_cell('App\Cells\MyClass::myMethod', ['param1' => 'value1', 'param2' => 'value2']) ?>

// Controlled Cell
<?= view_cell('App\Cells\MyCell', ['param1' => 'value1', 'param2' => 'value2']) ?>
2 changes: 1 addition & 1 deletion user_guide_src/source/outgoing/view_cells/014.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AlertMessageCell extends Cell
protected $message;
private $computed;

public function mount()
public function mount(): void
{
$this->computed = sprintf('%s - %s', $this->type, $this->message);
}
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/outgoing/view_cells/016.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class RecentPostsCell extends Cell
{
protected $posts;

public function linkPost($post)
public function linkPost($post): string
{
return anchor('posts/' . $post->id, $post->title);
}
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/outgoing/view_cells/018.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class RecentPostsCell extends Cell
{
protected $posts;

public function mount()
public function mount(): void
{
$this->posts = model('PostModel')->orderBy('created_at', 'DESC')->findAll(10);
}
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/outgoing/view_cells/019.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class RecentPostsCell extends Cell
{
protected $posts;

public function mount(?int $categoryId)
public function mount(?int $categoryId): void
{
$this->posts = model('PostModel')
->when(
Expand Down

0 comments on commit bfa6137

Please sign in to comment.