标签: cakephp-4.x

仅对管理员前缀 CakePHP 4 进行身份验证

嘿伙计们!

我没有使用过第 2 代的 Cake,所以我对所有新东西都有些厌烦。我将身份验证组件添加到我的项目中,它工作正常(登录/退出/传递哈希等),但我似乎无法找到如何仅使用“管理员”前缀来要求身份验证。

在过去是一些“简单”的东西,比如:

if($this->params[‘prefix’] == ‘admin’){
    $this->Auth->deny(’*’);
}
Run Code Online (Sandbox Code Playgroud)

authentication cakephp cakephp-4.x

3
推荐指数
1
解决办法
1393
查看次数

在cakephp4中如何访问模型中的模型

如何在 cakephp4.2 的模型中访问另一个模型?关于这个问题的文档对我来说不清楚,然后我可以对此运行查询吗?TableRegistry 现已弃用。

 error Unknown method "getTableLocator" called on App\Model\Table\LessonsTable  

//none of these no longer work
in model {

use Cake\ORM\Locator\LocatorAwareTrait;


class LessonsTable extends Table
 {
 ..

  private function  getlessonRevenue(){

  //$clients = $this->getTableLocator()->get('Clients');
  // $cleints = TableRegistry::get('Clients'); 
  // $this->Table = TableRegistry::get('Clients');
   $clients = $this->getTableLocator()->get('Clients');
Run Code Online (Sandbox Code Playgroud)

https://api.cakephp.org/4.0/class-Cake.ORM.TableRegistry.html

cakephp cakephp-3.0 cakephp-4.x

3
推荐指数
1
解决办法
1430
查看次数

CakePHP 4.1.4 - 如何在新版 CakePHP 中创建、读取和检查 cookie

在 CakePHP 3.8 中,我的控制器部分如下所示:

// ...  

public function beforeFilter(Event $event)
{
    // ...

    $this->Cookie->configKey('guestCookie', [
        'expires' => '+1 days',
        'httpOnly' => true,
        'encryption' => false
    ]);

    if(!$this->Cookie->check('guestCookie')) {
        $guestID = Text::uuid();
        $this->Cookie->write('guestCookie', $guestID);
    }
    
    $this->guestCookie = $this->Cookie->read('guestCookie');

    // ...
}


public function error()
{
    // ...
    $this->Cookie->delete('guestCookie');
    // ...
}

// ...
Run Code Online (Sandbox Code Playgroud)

在 CakePHP4 版本中如何写同样的东西?我的问题与定义 Cookie 相关。Cookie 设置在这里描述: https://book.cakephp.org/4/en/controllers/request-response.html#cookie-collections ,但不幸的是这根本没有帮助我。

我试图以这种方式解决问题:

    public function beforeFilter(EventInterface $event)
    {
//...
    
        if(!$this->cookies->has('guestCookie')) { 
            $cookie = (new Cookie('guestCookie'))->withValue(Text::uuid())->withExpiry(new \DateTime('+20 days'))->withPath('/')->withSecure(false)->withHttpOnly(true); 
            $this->cookies = new CookieCollection([$cookie]);
        } …
Run Code Online (Sandbox Code Playgroud)

cookies cakephp cakephp-4.x

2
推荐指数
1
解决办法
967
查看次数

CakePHP 4.1 用户实体作为授权身份关联字段

我刚刚在 CakePHP 4.1 中创建了一个非常小的项目,主要模仿 CMS 教程,并且想要实现一个相当简单的逻辑。使用该Authorization模块,我希望允许用户A能够访问view用户,B如果 1) 他们实际上是同一用户 ( A = B) 或 2) 如果A是管理员。

有两个数据库表 -usersuser_types。有users一个外键.user_type_iduser_types

这种关系在代码中体现为:

##### in UsersTable.php #####

class UsersTable extends Table {
    public function initialize(array $config): void
    {
        parent::initialize($config);

        $this->setTable('users');
        $this->setDisplayField('name');
        $this->setPrimaryKey('id');
        $this->belongsTo('UserTypes');

        $this->addBehavior('Timestamp');
    }

    //...
}



##### in UserTypesTable.php #####

class UserTypesTable extends Table {
    public function initialize(array $config): void
    {
        parent::initialize($config);

        $this->setTable('user_types');
        $this->setDisplayField('name');
        $this->setPrimaryKey('id'); …
Run Code Online (Sandbox Code Playgroud)

orm entity cakephp cakephp-4.x

2
推荐指数
1
解决办法
2536
查看次数

无法访问组件中的请求对象。升级到cakephp 4.0后组件中$this->request为空

如何访问 cakephp 4.0 组件中的请求对象和会话数据?

我在组件中的以下代码中升级后 出现以下错误Call to a member function getSession() on null 。$session = $this->request->getSession();

php cakephp cakephp-4.x

1
推荐指数
1
解决办法
625
查看次数

CakePHP:文件上传时无法将类 Laminas\Dictoros\UploadedFile 的对象转换为字符串

我正在尝试在 cakephp 版本 4 中上传文件。

我正在关注这个文档

我在控制器中尝试过

if ($this->request->is('post')) {

            $image = $this->request->getData('image');
            $fileName = $image->getClientFilename();

            $targetPath = WWW_ROOT.'img'.DS.$fileName;

            $image->moveTo($targetPath);

            $user = $this->Users->patchEntity($user, $this->request->getData());   //line 58

            $user->image = $fileName;

            $this->Users->save($user);
}
Run Code Online (Sandbox Code Playgroud)

图片上传工作正常,名称也保存在数据库中。但是当发生验证错误时,我得到

Warning (4096): Object of class Laminas\Diactoros\UploadedFile could not be converted to string [CORE\src\Database\Type\StringType.php, line 97]
Run Code Online (Sandbox Code Playgroud)

日志

Cake\Database\Type\StringType::marshal() - CORE\src\Database\Type\StringType.php, line 97
Cake\ORM\Marshaller::Cake\ORM\{closure}() - CORE\src\ORM\Marshaller.php, line 78
Cake\ORM\Marshaller::merge() - CORE\src\ORM\Marshaller.php, line 558
Cake\ORM\Table::patchEntity() - CORE\src\ORM\Table.php, line 2761
App\Controller\UsersController::add() - APP/Controller\UsersController.php, line 58
Cake\Controller\Controller::invokeAction() - CORE\src\Controller\Controller.php, line 524
Cake\Controller\ControllerFactory::invoke() …
Run Code Online (Sandbox Code Playgroud)

cakephp cakephp-4.x

0
推荐指数
1
解决办法
2877
查看次数

标签 统计

cakephp ×6

cakephp-4.x ×6

authentication ×1

cakephp-3.0 ×1

cookies ×1

entity ×1

orm ×1

php ×1