首页 文章

了解Cake PHP路由

提问于
浏览
0

我来自Laravel,现在我有一项基于Cake 3的工作要做 . 我需要帮助来理解路由 . 该应用程序使用CRUD为Angular5应用程序生成API . routes.php php是这样的:

<?php
/**
 * Routes configuration
 *
 * In this file, you set up routes to your controllers and their actions.
 * Routes are very important mechanism that allows you to freely connect
 * different URLs to chosen controllers and their actions (functions).
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */

use Cake\Core\Plugin;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
use Cake\Routing\Route\DashedRoute;

Router::defaultRouteClass(DashedRoute::class);

Router::extensions(['json', 'xml']);

Router::scope($baseScope, function ($routes) {

    $routes->connect('/', ['controller' => 'App', 'action' => 'welcome']);
    $routes->connect('/getConfig', ['controller' => 'App', 'action' => 'getConfig']);

    $routes->fallbacks('DashedRoute');
});

/**
 * Load all plugin routes.  See the Plugin documentation on
 * how to customize the loading of plugin routes.
 */
Plugin::routes();

我需要知道哪种方法,例如,响应用户/添加路由....在用户控制器内部我没有得到任何添加方法...

1 回答

  • 1

    如果你来自Laravel,很明显会混淆路由 . CakePhp的路由不同,但在某些方面使用Laravel路由更容易 .

    在Laravel中,您必须根据需要为请求类型(get,post)定义控制器的每个方法的路由 .

    但是在CakePhp中,如果你想使用CakePhp的约定,你不需要为控制器的每个方法编写 route .

    如果你的控制器是UsersController并且方法是add,那么通过CakePhp约定路由将是'users / add' .

    同样的,

    Controller: Articles, Method:index => route: articles/index
    Controller: Articles, Method:view_user_data => route: articles/view-user-data
    

    更多这里:https://book.cakephp.org/3.0/en/development/routing.html

    还要看看cakephp约定的力量:https://book.cakephp.org/3.0/en/intro/conventions.html

相关问题