CakePHP错误:在此服务器上找不到请求的地址'/ admin / news / add'

时间:2017-07-21 22:20:22

标签: php cakephp

/app/Controller/NewsController.php

<?php

App::uses('AppController', 'Controller');

class NewsController extends AppController {

public $uses = array('Pages', 'News');


public function admin_index() {
    $this->paginate = array(
        'News' => array(
            'id' => 'News.created DESC',
            'content' => array('content'),
            'limit'=>50
        )
    );
    $news = $this->paginate('News');
    $this->set('news',$news);
}

public function admin_add() {
    if ($this->request->is('post') || !empty($this->request->data)) {
        $this->loadModel('News');
        $n = $this->News->findById($this->request->data('News.id'));
        if (empty($n)) {
            $this->Session->setFlash('User not found', 'flash', array('class' => 'danger'));
            $this->redirect($this->referer());
        } else {
            $this->request->data['News']['id'] = $n['News']['id'];
            $this->News->id = $n['News']['id'];
            $this->News->saveField('content',$n['News']['content']);
            $this->News->saveField('date',$n['News']['date']);
            $this->News->saveField('slug','enabled');
            $this->News->create();
            $this->News->save($this->request->data);
        }
        return $this->redirect($this->referer());
    }
    exit(0);
}

}

/app/View/News/admin_index.ctp

<div class="text-center" style="border: 1px solid #B1B1B1;padding: 20px;border-radius: 6px;box-shadow: 0px 0px 6px #D1D1D1;font-family: Trebuchet MS, Arial, Helvetica, sans-serif;">
<?php echo $this->Form->create('News',array('url'=>array('action'=>'add'),'class'=>'form-inline'));?>
    <div class="form-group">
        <?php echo $this->Form->textarea('content',array('label'=>false,'placeholder'=>'News to update','required','class'=>'form-control')); ?>
    </div><br><br>
    <div class="form-group">
        <?php echo $this->Form->input('date',array('label'=>false,'placeholder'=>'YYYY-MM-DD','required','class'=>'form-control')); ?>
    </div>
    <br>
    <br>

SQL表 表名:新闻 结构:

  1. AI - id
  2. Varchar - content
  3. 日期 - 日期
  4. Varchar - slug
  5. 我正在尝试将新闻添加到我的数据库,但我收到错误。我将添加的每条新闻都将在新行中插入数据库。

    错误记录

        2017-07-23 16:00:22 Error: [BadRequestException] The request has been black-holed
        Request URL: /admin/news/add
        Stack Trace:
        #0 /home/username/public_html/socialdealers.in/lib/Cake/Controller/Component/SecurityComponent.php(239): SecurityComponent->blackHole(Object(NewsController), 'auth')
        #1 [internal function]: SecurityComponent->startup(Object(NewsController))
        #2 /home/username/public_html/socialdealers.in/lib/Cake/Utility/ObjectCollection.php(132): call_user_func_array(Array, Array)
        #3 [internal function]: ObjectCollection->trigger(Object(CakeEvent))
        #4 /home/username/public_html/socialdealers.in/lib/Cake/Event/CakeEventManager.php(247): call_user_func(Array, Object(CakeEvent))
        #5 /home/username/public_html/socialdealers.in/lib/Cake/Controller/Controller.php(675): CakeEventManager->dispatch(Object(CakeEvent))
        #6 /home/username/public_html/socialdealers.in/lib/Cake/Routing/Dispatcher.php(182): Controller->startupProcess()
        #7 /home/username/public_html/socialdealers.in/lib/Cake/Routing/Dispatcher.php(160): Dispatcher->_invoke(Object(NewsController), Object(CakeRequest), Object(CakeResponse))
        #8 /home/username/public_html/socialdealers.in/app/webroot/index.php(108): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))
        #9 {main}

    应用/配置/ routes.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
     * @package       app.Config
     * @since         CakePHP(tm) v 0.2.9
     * @license       http://www.opensource.org/licenses/mit-license.php MIT License
     */
    /**
     * Here, we are connecting '/' (base path) to controller called 'Pages',
     * its action called 'display', and we pass a param to select the view file
     * to use (in this case, /app/View/Pages/home.ctp)...
     */
    Router::connect('/apis/*', array('controller' => 'apis', 'action' => 'index'));
    Router::connect('/api.php/*', array('controller' => 'apis', 'action' => 'index'));
    Router::connect('/pages/api', array('controller' => 'apis', 'action' => 'apihelp'));
    
    Router::connect('/', array('controller' => 'users', 'action' => 'login'));
    /**
     * ...and connect the rest of 'Pages' controller's URLs.
     */
    $prefix = 'admin';
    
    //Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
    
    Router::connect("/{$prefix}/:controller", array('action' => 'index', 'prefix' => $prefix, $prefix => true));
    
    Router::connect("/{$prefix}/:controller/:action/*", array('prefix' => $prefix, $prefix => true));
    
    /**
     * Load all plugin routes. See the CakePlugin documentation on
     * how to customize the loading of plugin routes.
     */
    CakePlugin::routes();
    
    /**
     * Load the CakePHP default routes. Only remove this if you do not want to use
     * the built-in default routes.
     */
    require CAKE . 'Config' . DS . 'routes.php';
    

2 个答案:

答案 0 :(得分:0)

检查您的路由是否正确。您正试图达到“admin”前缀动作“添加”,因此此错误意味着路由可能有问题。

尝试在路由数组中指定前缀。而不是:

'url'=>array('action'=>'add')

尝试使用:

'url'=>array('action'=>'add', 'prefix' => 'admin')

更多信息:CakePHP 2 Prefix Routing

答案 1 :(得分:0)

问题解决了。 感谢 Szymon 解决我的大部分问题。

编辑NewsController.php

$data['News']['date'] = $this->request->data['News']['date']['year'].'-'.$this->request->data['News']['date']['month'].'-'.$this->request->data['News']['date']['day'];