了解Cake PHP路由

时间:2018-07-24 07:43:19

标签: cakephp cakephp-3.0

我来自Laravel,现在我要基于Cake 3做一些工作。我需要帮助来理解路由。该应用程序使用CRUD为Angular5应用程序生成API。 route.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 个答案:

答案 0 :(得分:1)

  

如果您来自哪里,很显然会对路由感到困惑   Laravel。

     

CakePhp的路由不同,但在某些情况下使用Laravel路由更容易   方式。

在Laravel中,您必须像所知道的那样为请求类型(get,post)的控制器的每个方法定义路由。

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

  

如果您的控制器是UsersController,并且添加了方法,则由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