我正在使用CodeIgniter 4的最新“主”分支
我有一个正在尝试自动加载的库。实际上,我希望拥有“一个”index.php(具有元数据、基本 html 结构等),通过它我可以通过我的“模板”库加载视图。
我的库文件:(~/app/Libraries/Template.php)
//class Template extends CI_Controller
class Template {
/* This throws an error, but I will open up a separte thread for this
public function __construct() {
parent::__construct();
}
*/
public function render($view, $data = array()) {
$data['content_view'] = $view;
return view('layout/index', $data);
}
}
Run Code Online (Sandbox Code Playgroud)
我还设置了一个控制器:
class Locations extends BaseController
{
public function index()
{
return $this->template->render("locations/index", $view_data);
//return view('locations/index');
}
//--------------------------------------------------------------------
}
Run Code Online (Sandbox Code Playgroud)
在 ~/app/Config/ 我添加了我的库
$classmap = [
'Template' => APPPATH .'/Libraries/Template.php' …Run Code Online (Sandbox Code Playgroud) $data['trafik'] = new stdClass;
Run Code Online (Sandbox Code Playgroud)
这向我显示错误:
找不到类“App\Controllers\stdClass”
我刚刚创建了一个 codeigniter4 项目,我想将其上传到 sharehosting 上。但我的网站无法访问,它给出了服务器错误。
我知道我们可以通过钩子在 CI3 中缩小 HTML,但在 CI 4 中
我通过在每个方法的返回之前添加一个 minify 函数来完成此操作。
minifyHTML(view('admin/template/template',$this->data));
Run Code Online (Sandbox Code Playgroud)
有什么其他方法可以在不到处使用 minify 函数的情况下做到这一点吗?
我还找到了另一个解决方案,即在 BaseConroller 中添加一个模板函数来呈现所有视图。但我已经在项目的很多地方使用了 view() ,它对我来说不可行,但对其他人来说可以。
我已经尝试了多种方法来实现此目的,但似乎都不起作用,似乎 CodeIgniter 4 无法将多个过滤器应用于单个路由,目前这是我正在尝试的:
提供InfoFilter.php:
<?php namespace App\Filters;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;
class ProvideinfoFilter implements FilterInterface
{
public function before(RequestInterface $request, $arguments = null)
{
echo "pinfo";
$account_data = new \App\Libraries\Account_Data;
return $account_data->no_info_redirect();
}
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
}
}
Run Code Online (Sandbox Code Playgroud)
访问过滤器.php:
<?php namespace App\Filters;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;
class AccessFilter implements FilterInterface
{
public function before(RequestInterface $request, $arguments = null)
{
$account_data = new \App\Libraries\Account_Data;
echo "accessf";
if ($request->uri->getSegment(1) !== …Run Code Online (Sandbox Code Playgroud) 当我第一次为 table 制作迁移文件时users,public function down()迁移文件中的 是空的。当我运行时,php spark migrate表users已创建。
然后我用 生成了另一个迁移文件php spark make:migration users,根据新的表结构做了一些调整并放入$this->forge->dropTable('users');了public function down(). 但是当我php spark migrate再次运行时,该users表没有新字段..
我正在使用 codeigniter 4 和 mysql。这是我的代码
用户模型php
<?php
namespace App\Models;
use CodeIgniter\Model;
class UserModel extends Model
{
protected $DBGroup = 'default';
protected $table = 'users';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $insertID = 0;
protected $returnType = 'array';
protected $useSoftDeletes = false; …Run Code Online (Sandbox Code Playgroud) 我正在将我的项目从 CodeIgniter 3 迁移到 CodeIgniter 4。我对框架的新结构感到困惑。所以这是我的问题,
我在App.php 中将我的基本网址设置为:
protected $proj_root= "http://".$_SERVER['HTTP_HOST'];
protected $proj_root2 = str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
protected $mybase= $proj_root.$proj_root2;
public $baseURL = $mybase;
Run Code Online (Sandbox Code Playgroud)
但我收到这样的错误:
致命错误:常量表达式在第 26 行的 D:\xampp\htdocs\delivery_dashboard\app\Config\App.php 中包含无效操作
所以从字面上看,我只能这样做:
public $baseURL = "http://localhost/my_project/"
Run Code Online (Sandbox Code Playgroud)
如何使用动态设置我的基本 url$_SERVER['HTTP_HOST']或者这里有任何解决方法?
谢谢您的帮助!
我安装了 Codeigniter 4.0.2 并做了以下更改:
1-CI_ENVIRONMENT = development在 .env 文件中
2-SetEnv CI_ENVIRONMENT development在公用文件夹中的 .htaccess
3-$useKint = true;在公共文件夹中的主 index.php 文件中添加
当我在本地主机上打开时,会呈现欢迎页面,但不会呈现任何调试工具栏。我错过了什么吗?
在 CI3 中,我们制作自定义库并在 config/autoload.php 中自动加载它,然后我们可以在控制器、模型、视图中的任何地方使用它,通过简单的 $this->rules->status() 其中 Rules 是应用程序/库中的库文件夹
但是无法在 CI 4 中做同样的事情,是否有其他可用的替代方法
我的 Codeigniter 4 验证错误消息有一些问题,验证正在工作,但错误消息没有显示,我不知道出了什么问题。下面是我的代码:
控制器
public function register()
{
$data = [];
helper(['form']);
// To add in is_unique
if($this->request->getMethod() == 'post'){
//validations
$rules = [
'username' => 'required',
'email' => 'required|valid_email',
'firstname' => 'required',
'lastname' => 'required',
'dob' => 'required',
'country' => 'required',
'contact' => 'required',
'password' => 'required'
];
if(!$this->validate($rules)){
$data['validation'] = $this->validator;
}else{
//store information into database
}
}
echo view('templates/header', $data);
echo view('account/register');
echo view('templates/footer');
}
Run Code Online (Sandbox Code Playgroud)
看法
<?php if(isset($validation)): ?>
<div class="form-group col-12 col-md-12">
<div class="alert alert-danger" …Run Code Online (Sandbox Code Playgroud) 所以我的控制器上有这个功能来检查用户是否登录,但当用户未登录时它不会重定向到登录页面
public function __construct()
{
if(session()->get('logged_in') !== true)
{
redirect('login');
}
}
Run Code Online (Sandbox Code Playgroud)
有人有解决方案吗?或者我应该对每个功能进行会话检查?喜欢
public function examplefunction()
{
if(session()->get('logged_in') !== true)
{
redirect('login');
}
//run other codes
}
Run Code Online (Sandbox Code Playgroud) CodeIgniter\Exceptions\FrameworkException 框架需要安装和加载以下扩展:{0}。SYSTEMPATH\CodeIgniter.php 第 224 行
当我使用以下代码创建令牌时,我尝试在 CI4 中使用 JWT 创建一个 Restful api:
$key = getenv('TOKEN_SECRET');
$payload = array(
"iat" => 1356999524,
"nbf" => 1357000000,
"uid" => $user['id'],
"email" => $user['email']
);
$token = JWT::encode($payload, $key);
Run Code Online (Sandbox Code Playgroud)
为什么我会收到错误:
预计有 3 个参数。找到 2
codeigniter-4 ×13
codeigniter ×7
php ×5
autoload ×1
base-url ×1
controller ×1
debugging ×1
filter ×1
jwt ×1
kint ×1
migration ×1
php-7 ×1
routes ×1
validation ×1