我有一个使用 CodeIgniter 4 的项目。
我有一个before过滤器:
public function before(RequestInterface $request){
$auth = $request->getHeader("Authorization");
if($auth == null){
$response = Services::response();
$response->setStatusCode(401);
$response->setHeader("WWW-Authenticate", "Bearer Token");
$response->setBody("{\"error\": \"unauthorized\"}");
exit;
}
}
Run Code Online (Sandbox Code Playgroud)
如果没有授权,我想停止请求并返回$response给客户端。
现在请求已停止,因为exit.
但作为答案我得到了200 OK并且没有body content。
我如何发出exit请求并将其设置$response给客户端?
我正在尝试使用 Xampp 设置 Codeigniter4,但是当调用CodeIgniter4 框架文件http://localhost/projectfolder/public/index.php中所述的公共地址时,出现下一个错误:README.md
<br />
<b>Fatal error</b>: Uncaught Error: Call to undefined function CodeIgniter\locale_set_default() in
C:\xampp\htdocs\codeigniter4\system\CodeIgniter.php:184
Stack trace:
#0 C:\xampp\htdocs\codeigniter4\system\bootstrap.php(181): CodeIgniter\CodeIgniter->initialize()
#1 C:\xampp\htdocs\codeigniter4\public\index.php(36): require('C:\\xampp\\htdocs...')
#2 {main}
thrown in <b>C:\xampp\htdocs\codeigniter4\system\CodeIgniter.php</b> on line <b>184</b><br />
Run Code Online (Sandbox Code Playgroud)
我已经尝试过这个解决方案,但它对我不起作用。
有人知道如何解决吗?
如果您有解决此 codeigniter4 错误的方法,请帮助在此处输入图像描述
Codeigniter4 主页无法显示但显示该错误
我将 Twig 与 Symfony 一起使用,我真的很喜欢它。我现在有一个 CodeIgniter 项目,我想将 Twig 与其集成。
我通过 Composer 安装了最新版本的 CodeIgniter 和 Twig,并遵循了本教程,但我相信教程中的代码适用于 CI v3。
任何将 Twig 与 CI v4 集成的人都可以帮我提供正确的代码吗?
解决方案如下!
如何验证 codeigniter 4 中的可选值
例如条件
我应该如何在第一个条件下验证
这是我的模型代码,我希望电话字段是可选的
'create' => [
'message' => 'required|alpha_numeric_punct|max_length[10000]',
'name' => 'required|alpha_space',
'email' => 'required|valid_email',
'phone' => 'alpha_numeric_punct',
'status' => 'required|integer',
],
Run Code Online (Sandbox Code Playgroud) 通常,如果我想更改 CI4 项目中的数据库主机名,我将在 .env 文件中更改它并更改
database.default.hostname = localhost
但现在我需要在环境中使用 MYSQL_HOST 来更改主机名,如下所示
MYSQL_HOST = localhost
我可以在 CI4 中这样做吗?如果我将 Database.php 文件更改为,则会出现错误
public $default = [
'DSN' => '',
'hostname' => getenv('MYSQL_HOST'),
'username' => '',
'password' => '',
'database' => '',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug' => (ENVIRONMENT !== 'production'),
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port' => 3306,
];
Run Code Online (Sandbox Code Playgroud) 我刚刚使用 Composer 将我的 Codeigniter 4 站点从 4.1 更新到 4.2,现在在 Apache 错误日志中收到以下服务器错误:
PHP Fatal error: Uncaught Error: Call to a member function run() on int in /var/www/vhosts/site/public/index.php
Run Code Online (Sandbox Code Playgroud)
我认为文件已更改或 CI 4.2 需要缺少 PHP 库,但我查看了 .env、/public/index.php 和 /app/config/App.php 文件,它们没有更改。
任何帮助将非常感激!
我form_open()在视图中使用辅助方法来指示控制器方法来处理表单提交操作并在app\Config\Routes.php.
我仍然收到错误Can't find a route for 'registrations/index'。
请协助解决该问题。下面提供了代码片段。
register.php查看文件: <?php echo form_open('/registrations/index'); ?>
Run Code Online (Sandbox Code Playgroud)
Registrations.php控制器:class Registrations extends BaseController {
public function index() {
$data['coursename'] = $this->getCourseName();
log_message('info','name field >' . $this->request->getVar('iname') . '<<');
echo view('templates/header');
echo view('pages/register', $data);
echo view('templates/footer');
}
Run Code Online (Sandbox Code Playgroud)
路线.php
$routes->get('/registrations/index', 'Registrations::index');
Run Code Online (Sandbox Code Playgroud) 我正在使用 codeigniter 4,我尝试实现使用 Google 登录。到目前为止一切顺利,直到我尝试获得结果时出现此错误。这是错误消息:
Fatal error: Declaration of Monolog\Logger::emergency(Stringable|string $message, array $context = []): void must be compatible with Psr\Log\LoggerInterface::emergency($message, array $context = []) in C:\xampp\htdocs\postlite\app\Libraries\vendor\monolog\monolog\src\Monolog\Logger.php on line 669
有人可以帮我吗?
codeigniter-4 ×10
codeigniter ×6
php ×6
form-submit ×1
oauth-2.0 ×1
routes ×1
twig ×1
xampp ×1