Skip to content

Commit ca7335c

Browse files
authored
Merge pull request #565 from lonnieezell/action-render
dev: Allow easier overriding of views in developer applications. Fixes #555
2 parents 1351be6 + 64fbd7a commit ca7335c

File tree

7 files changed

+66
-13
lines changed

7 files changed

+66
-13
lines changed

docs/customization.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- [Route Configuration](#route-configuration)
55
- [Custom Redirect URLs](#custom-redirect-urls)
66
- [Extending the Controllers](#extending-the-controllers)
7+
- [Integrating Custom View Libraries](#integrating-custom-view-libraries)
78
- [Custom Validation Rules](#custom-validation-rules)
89
- [Registration](#registration)
910
- [Login](#login)
@@ -90,6 +91,25 @@ class LoginController extends ShieldLogin
9091
}
9192
```
9293

94+
## Integrating Custom View Libraries
95+
96+
If your application uses a different method to convert view files to HTML than CodeIgniter's built-in `view()` helper you can easily integrate your system anywhere that a view is rendered within Shield. All controllers and actions use the `CodeIgniter\Shield\Traits\Viewable` trait which provides a simple `view()` method that takes the same arguments as the `view()` helper. This allows you to extend the Action or Controller and only change the single method of rendering the view, leaving all of the logic untouched so your app will not need to maintain Shield logic when it doesn't need to change it.
97+
98+
```php
99+
use Acme\Themes\Traits\Themeable;
100+
use CodeIgniter\Shield\Controllers\LoginController;
101+
102+
class MyLoginController extends LoginController
103+
{
104+
use Themable;
105+
106+
protected function view(string $view, array $data = [], array $options = []): string
107+
{
108+
return $this->themedView($view, $data, $options);
109+
}
110+
}
111+
```
112+
93113
## Custom Validation Rules
94114

95115
### Registration
@@ -149,7 +169,7 @@ Similar to the process for validation rules in the **Registration** section, you
149169

150170
```php
151171
//--------------------------------------------------------------------
152-
// Rules For Login
172+
// Rules For Login
153173
//--------------------------------------------------------------------
154174
public $login = [
155175
// 'username' => [

src/Authentication/Actions/Email2FA.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use CodeIgniter\Shield\Entities\UserIdentity;
1313
use CodeIgniter\Shield\Exceptions\RuntimeException;
1414
use CodeIgniter\Shield\Models\UserIdentityModel;
15+
use CodeIgniter\Shield\Traits\Viewable;
1516

1617
/**
1718
* Class Email2FA
@@ -20,6 +21,8 @@
2021
*/
2122
class Email2FA implements ActionInterface
2223
{
24+
use Viewable;
25+
2326
private string $type = Session::ID_TYPE_EMAIL_2FA;
2427

2528
/**
@@ -38,7 +41,7 @@ public function show(): string
3841

3942
$this->createIdentity($user);
4043

41-
return view(setting('Auth.views')['action_email_2fa'], ['user' => $user]);
44+
return $this->view(setting('Auth.views')['action_email_2fa'], ['user' => $user]);
4245
}
4346

4447
/**
@@ -81,7 +84,7 @@ public function handle(IncomingRequest $request)
8184
$email = emailer()->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '');
8285
$email->setTo($user->email);
8386
$email->setSubject(lang('Auth.email2FASubject'));
84-
$email->setMessage(view(setting('Auth.views')['action_email_2fa_email'], ['code' => $identity->secret, 'ipAddress' => $ipAddress, 'userAgent' => $userAgent, 'date' => $date]));
87+
$email->setMessage($this->view(setting('Auth.views')['action_email_2fa_email'], ['code' => $identity->secret, 'ipAddress' => $ipAddress, 'userAgent' => $userAgent, 'date' => $date]));
8588

8689
if ($email->send(false) === false) {
8790
throw new RuntimeException('Cannot send email for user: ' . $user->email . "\n" . $email->printDebugger(['headers']));
@@ -90,7 +93,7 @@ public function handle(IncomingRequest $request)
9093
// Clear the email
9194
$email->clear();
9295

93-
return view(setting('Auth.views')['action_email_2fa_verify']);
96+
return $this->view(setting('Auth.views')['action_email_2fa_verify']);
9497
}
9598

9699
/**
@@ -116,7 +119,7 @@ public function verify(IncomingRequest $request)
116119
if (! $authenticator->checkAction($identity, $postedToken)) {
117120
session()->setFlashdata('error', lang('Auth.invalid2FAToken'));
118121

119-
return view(setting('Auth.views')['action_email_2fa_verify']);
122+
return $this->view(setting('Auth.views')['action_email_2fa_verify']);
120123
}
121124

122125
// Get our login redirect url

src/Authentication/Actions/EmailActivator.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
use CodeIgniter\Shield\Exceptions\LogicException;
1616
use CodeIgniter\Shield\Exceptions\RuntimeException;
1717
use CodeIgniter\Shield\Models\UserIdentityModel;
18+
use CodeIgniter\Shield\Traits\Viewable;
1819

1920
class EmailActivator implements ActionInterface
2021
{
22+
use Viewable;
23+
2124
private string $type = Session::ID_TYPE_EMAIL_ACTIVATE;
2225

2326
/**
@@ -55,7 +58,7 @@ public function show(): string
5558
$email = emailer()->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '');
5659
$email->setTo($userEmail);
5760
$email->setSubject(lang('Auth.emailActivateSubject'));
58-
$email->setMessage(view(setting('Auth.views')['action_email_activate_email'], ['code' => $code, 'ipAddress' => $ipAddress, 'userAgent' => $userAgent, 'date' => $date]));
61+
$email->setMessage($this->view(setting('Auth.views')['action_email_activate_email'], ['code' => $code, 'ipAddress' => $ipAddress, 'userAgent' => $userAgent, 'date' => $date]));
5962

6063
if ($email->send(false) === false) {
6164
throw new RuntimeException('Cannot send email for user: ' . $user->email . "\n" . $email->printDebugger(['headers']));
@@ -65,7 +68,7 @@ public function show(): string
6568
$email->clear();
6669

6770
// Display the info page
68-
return view(setting('Auth.views')['action_email_activate_show'], ['user' => $user]);
71+
return $this->view(setting('Auth.views')['action_email_activate_show'], ['user' => $user]);
6972
}
7073

7174
/**
@@ -102,7 +105,7 @@ public function verify(IncomingRequest $request)
102105
if (! $authenticator->checkAction($identity, $postedToken)) {
103106
session()->setFlashdata('error', lang('Auth.invalidActivateToken'));
104107

105-
return view(setting('Auth.views')['action_email_activate_show']);
108+
return $this->view(setting('Auth.views')['action_email_activate_show']);
106109
}
107110

108111
$user = $authenticator->getUser();

src/Controllers/LoginController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
use App\Controllers\BaseController;
88
use CodeIgniter\HTTP\RedirectResponse;
99
use CodeIgniter\Shield\Authentication\Authenticators\Session;
10+
use CodeIgniter\Shield\Traits\Viewable;
1011

1112
class LoginController extends BaseController
1213
{
14+
use Viewable;
15+
1316
protected $helpers = ['setting'];
1417

1518
/**
@@ -31,7 +34,7 @@ public function loginView()
3134
return redirect()->route('auth-action-show');
3235
}
3336

34-
return view(setting('Auth.views')['login']);
37+
return $this->view(setting('Auth.views')['login']);
3538
}
3639

3740
/**

src/Controllers/MagicLinkController.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use CodeIgniter\Shield\Models\LoginModel;
1414
use CodeIgniter\Shield\Models\UserIdentityModel;
1515
use CodeIgniter\Shield\Models\UserModel;
16+
use CodeIgniter\Shield\Traits\Viewable;
1617

1718
/**
1819
* Handles "Magic Link" logins - an email-based
@@ -24,6 +25,8 @@
2425
*/
2526
class MagicLinkController extends BaseController
2627
{
28+
use Viewable;
29+
2730
/**
2831
* @var UserModel
2932
*/
@@ -48,7 +51,7 @@ public function loginView()
4851
return redirect()->to(config('Auth')->loginRedirect());
4952
}
5053

51-
return view(setting('Auth.views')['magic-link-login']);
54+
return $this->view(setting('Auth.views')['magic-link-login']);
5255
}
5356

5457
/**
@@ -102,7 +105,7 @@ public function loginAction()
102105
$email = emailer()->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '');
103106
$email->setTo($user->email);
104107
$email->setSubject(lang('Auth.magicLinkSubject'));
105-
$email->setMessage(view(setting('Auth.views')['magic-link-email'], ['token' => $token, 'ipAddress' => $ipAddress, 'userAgent' => $userAgent, 'date' => $date]));
108+
$email->setMessage($this->view(setting('Auth.views')['magic-link-email'], ['token' => $token, 'ipAddress' => $ipAddress, 'userAgent' => $userAgent, 'date' => $date]));
106109

107110
if ($email->send(false) === false) {
108111
log_message('error', $email->printDebugger(['headers']));
@@ -121,7 +124,7 @@ public function loginAction()
121124
*/
122125
protected function displayMessage(): string
123126
{
124-
return view(setting('Auth.views')['magic-link-message']);
127+
return $this->view(setting('Auth.views')['magic-link-message']);
125128
}
126129

127130
/**

src/Controllers/RegisterController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use CodeIgniter\Shield\Entities\User;
1212
use CodeIgniter\Shield\Exceptions\ValidationException;
1313
use CodeIgniter\Shield\Models\UserModel;
14+
use CodeIgniter\Shield\Traits\Viewable;
1415

1516
/**
1617
* Class RegisterController
@@ -20,6 +21,8 @@
2021
*/
2122
class RegisterController extends BaseController
2223
{
24+
use Viewable;
25+
2326
protected $helpers = ['setting'];
2427

2528
/**
@@ -47,7 +50,7 @@ public function registerView()
4750
return redirect()->route('auth-action-show');
4851
}
4952

50-
return view(setting('Auth.views')['register']);
53+
return $this->view(setting('Auth.views')['register']);
5154
}
5255

5356
/**

src/Traits/Viewable.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodeIgniter\Shield\Traits;
6+
7+
trait Viewable
8+
{
9+
/**
10+
* Provides a way for third-party systems to simply override
11+
* the way the view gets converted to HTML to integrate with their
12+
* own templating systems.
13+
*/
14+
protected function view(string $view, array $data = [], array $options = []): string
15+
{
16+
return view($view, $data, $options);
17+
}
18+
}

0 commit comments

Comments
 (0)