【问题标题】:How to call plugin controller from url in cakephp 3如何从 cakephp 3 中的 url 调用插件控制器
【发布时间】:2017-11-11 17:54:52
【问题描述】:

我有一个名为 PanelAdmin 的管理面板插件。 这是 CategoriesController.php

<?php

namespace PanelAdmin\Controller;

use Cake\Controller\Controller;
use Cake\ORM\TableRegistry;

class CategoriesController extends AppController
{
public function initialize()

    {

        parent::initialize();

        $this->loadComponent('Flash'); // Include the FlashComponent

    }

public function index()

    {

        $this->set('topics', $this->categories->find('all'));

    }

    }

?>

这是 Template/Topics/index.ctp 中的主题视图

<h1>Blog topics</h1>

<p><?= $this->Html->link('Add Topic', ['action' => 'add']) ?></p>

<table>

    <tr>

        <th>Id</th>

        <th>Title</th>

        <th>Created</th>

        <th>Actions</th>

    </tr>

我想直接调用 PanelAdmin,它应该显示上面的视图,但现在我收到以下错误:

Error: CategoriesController could not be found.

它在主 src 文件夹中搜索,我希望它在我点击 http://localhost/multi_shopping/PanelAdmin 这个 url 时搜索到插件文件夹。

【问题讨论】:

    标签: cakephp cakephp-3.0


    【解决方案1】:

    您需要添加连接到 /PanelAdmin 的路由

    $routes->connect('/PanelAdmin', [
        'plugin' => 'PanelAdmin', 
        'controller' => 'Categories', 
        'action' => 'index'
    ]);
    

    【讨论】:

    • 这将被添加到插件 PanelAdmin 的 routes.php 文件或主 routes.php 文件中
    【解决方案2】:

    首先确保您的插件已加载https://book.cakephp.org/3.0/en/plugins.html#plugin-configuration

    你不需要在你的插件中定义路由来访问你在url中的插件控制器,cakephp默认路由会自动让你的控制器访问为/panel-admin/categories

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多