Cakephp找不到我的tag.ctp文件

时间:2016-01-14 13:04:05

标签: php cakephp cakephp-3.x

安装XAMPP服务器之后,我想在我的服务器上安装cakephp。到目前为止一切正常。我已经在cakephp教程

上的初学者教程中生成了脚手架代码

但是,我现在是时候检索带有特定标记的书签了,我正在逐步遵循这些代码,但是当我尝试通过其URL调用它时,它没有显示标记。它一直告诉我模板丢失了。它说标签.ctp文件尚未创建。

这让我感到恼火,因为我一直在仔细检查这些代码,看看我是否错过了什么,但没有。谢谢。

这是BookmarkController的代码

use App\Controller\AppController;

/**
 * Bookmarks Controller
 *
 * @property \App\Model\Table\BookmarksTable $Bookmarks
 */
class BookmarksController extends AppController
{
    public function tags()
    {
        // The 'pass' key is provided by CakePHP and contains all
        // the passed URL path segments in the request.
        $tags = $this->request->params['pass'];

        // Use the BookmarksTable to find tagged bookmarks.
        $bookmarks = $this->Bookmarks->find('tagged', [
            'tags' => $tags
        ]);

        // Pass variables into the view template context.
        $this->set([
            'bookmarks' => $bookmarks,
            'tags' => $tags
        ]);
    }

    /**
     * Index method
     *
     * @return void
     */
    public function index()
    {
        $this->paginate = [
            'contain' => ['Users']
        ];
        $this->set('bookmarks', $this->paginate($this->Bookmarks));
        $this->set('_serialize', ['bookmarks']);
    }

    /**
     * View method
     *
     * @param string|null $id Bookmark id.
     * @return void
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function view($id = null)
    {
        $bookmark = $this->Bookmarks->get($id, [
            'contain' => ['Users', 'Tags']
        ]);
        $this->set('bookmark', $bookmark);
        $this->set('_serialize', ['bookmark']);
    }

    /**
     * Add method
     *
     * @return void Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $bookmark = $this->Bookmarks->newEntity();
        if ($this->request->is('post')) {
            $bookmark = $this->Bookmarks->patchEntity($bookmark, $this->request->data);
            if ($this->Bookmarks->save($bookmark)) {
                $this->Flash->success(__('The bookmark has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The bookmark could not be saved. Please, try again.'));
            }
        }
        $users = $this->Bookmarks->Users->find('list', ['limit' => 200]);
        $tags = $this->Bookmarks->Tags->find('list', ['limit' => 200]);
        $this->set(compact('bookmark', 'users', 'tags'));
        $this->set('_serialize', ['bookmark']);
    }

    /**
     * Edit method
     *
     * @param string|null $id Bookmark id.
     * @return void Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function edit($id = null)
    {
        $bookmark = $this->Bookmarks->get($id, [
            'contain' => ['Tags']
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $bookmark = $this->Bookmarks->patchEntity($bookmark, $this->request->data);
            if ($this->Bookmarks->save($bookmark)) {
                $this->Flash->success(__('The bookmark has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The bookmark could not be saved. Please, try again.'));
            }
        }
        $users = $this->Bookmarks->Users->find('list', ['limit' => 200]);
        $tags = $this->Bookmarks->Tags->find('list', ['limit' => 200]);
        $this->set(compact('bookmark', 'users', 'tags'));
        $this->set('_serialize', ['bookmark']);
    }

    /**
     * Delete method
     *
     * @param string|null $id Bookmark id.
     * @return \Cake\Network\Response|null Redirects to index.
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $bookmark = $this->Bookmarks->get($id);
        if ($this->Bookmarks->delete($bookmark)) {
            $this->Flash->success(__('The bookmark has been deleted.'));
        } else {
            $this->Flash->error(__('The bookmark could not be deleted. Please, try again.'));
        }
        return $this->redirect(['action' => 'index']);
    }
}

这是BookMarkerTable代码

use App\Model\Entity\Bookmark;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
 * Bookmarks Model
 *
 * @property \Cake\ORM\Association\BelongsTo $Users
 * @property \Cake\ORM\Association\BelongsToMany $Tags
 */



class BookmarksTable extends Table
{

// The $query argument is a query builder instance.
// The $options array will contain the 'tags' option we passed
// to find('tagged') in our controller action.
public function findTagged(Query $query, array $options)
{
    return $this->find()
        ->distinct(['Bookmarks.id'])
        ->matching('Tags', function ($q) use ($options) {
            if (empty($options['tags'])) {
                return $q->where(['Tags.title IS' => null]);
            }
            return $q->where(['Tags.title IN' => $options['tags']]);
        });
}
    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */

// The $query argument is a query builder instance.
// The $options array will contain the 'tags' option we passed
// to find('tagged') in our controller action.

    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('bookmarks');
        $this->displayField('title');
        $this->primaryKey('id');

        $this->addBehavior('Timestamp');

        $this->belongsTo('Users', [
            'foreignKey' => 'user_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsToMany('Tags', [
            'foreignKey' => 'bookmark_id',
            'targetForeignKey' => 'tag_id',
            'joinTable' => 'bookmarks_tags'
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator
            ->add('id', 'valid', ['rule' => 'numeric'])
            ->allowEmpty('id', 'create');

        $validator
            ->allowEmpty('title');

        $validator
            ->allowEmpty('description');

        $validator
            ->allowEmpty('url');

        return $validator;
    }

    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->existsIn(['user_id'], 'Users'));
        return $rules;
    }
}

以下是包含src文件夹

的file.txt的内容
 C:\Users\Pamela\bookmarkers\src\Console
    C:\Users\Pamela\bookmarkers\src\Controller
    C:\Users\Pamela\bookmarkers\src\files.txt
    C:\Users\Pamela\bookmarkers\src\Model
    C:\Users\Pamela\bookmarkers\src\Shell
    C:\Users\Pamela\bookmarkers\src\Template
    C:\Users\Pamela\bookmarkers\src\View
    C:\Users\Pamela\bookmarkers\src\Console\Installer.php
    C:\Users\Pamela\bookmarkers\src\Controller\AppController.php
    C:\Users\Pamela\bookmarkers\src\Controller\BookmarksController.php
    C:\Users\Pamela\bookmarkers\src\Controller\Component
    C:\Users\Pamela\bookmarkers\src\Controller\PagesController.php
    C:\Users\Pamela\bookmarkers\src\Controller\TagsController.php
    C:\Users\Pamela\bookmarkers\src\Controller\UsersController.php
    C:\Users\Pamela\bookmarkers\src\Controller\Component\empty
    C:\Users\Pamela\bookmarkers\src\Model\Behavior
    C:\Users\Pamela\bookmarkers\src\Model\Entity
    C:\Users\Pamela\bookmarkers\src\Model\Table
    C:\Users\Pamela\bookmarkers\src\Model\Behavior\empty
    C:\Users\Pamela\bookmarkers\src\Model\Entity\Bookmark.php
    C:\Users\Pamela\bookmarkers\src\Model\Entity\Tag.php
    C:\Users\Pamela\bookmarkers\src\Model\Entity\User.php
    C:\Users\Pamela\bookmarkers\src\Model\Table\BookmarksTable.php
    C:\Users\Pamela\bookmarkers\src\Model\Table\TagsTable.php
    C:\Users\Pamela\bookmarkers\src\Model\Table\UsersTable.php
    C:\Users\Pamela\bookmarkers\src\Shell\ConsoleShell.php
    C:\Users\Pamela\bookmarkers\src\Template\Bookmarks
    C:\Users\Pamela\bookmarkers\src\Template\Element
    C:\Users\Pamela\bookmarkers\src\Template\Email
    C:\Users\Pamela\bookmarkers\src\Template\Error
    C:\Users\Pamela\bookmarkers\src\Template\Layout
    C:\Users\Pamela\bookmarkers\src\Template\Pages
    C:\Users\Pamela\bookmarkers\src\Template\Tags
    C:\Users\Pamela\bookmarkers\src\Template\Users
    C:\Users\Pamela\bookmarkers\src\Template\Bookmarks\add.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Bookmarks\edit.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Bookmarks\index.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Bookmarks\tags.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Bookmarks\view.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Element\Flash
    C:\Users\Pamela\bookmarkers\src\Template\Element\Flash\default.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Element\Flash\error.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Element\Flash\success.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Email\html
    C:\Users\Pamela\bookmarkers\src\Template\Email\text
    C:\Users\Pamela\bookmarkers\src\Template\Email\html\default.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Email\text\default.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Error\error400.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Error\error500.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Layout\ajax.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Layout\default.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Layout\Email
    C:\Users\Pamela\bookmarkers\src\Template\Layout\error.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Layout\rss
    C:\Users\Pamela\bookmarkers\src\Template\Layout\Email\html
    C:\Users\Pamela\bookmarkers\src\Template\Layout\Email\text
    C:\Users\Pamela\bookmarkers\src\Template\Layout\Email\html\default.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Layout\Email\text\default.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Layout\rss\default.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Pages\home.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Tags\add.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Tags\edit.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Tags\index.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Tags\view.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Users\add.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Users\edit.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Users\index.ctp
    C:\Users\Pamela\bookmarkers\src\Template\Users\view.ctp
    C:\Users\Pamela\bookmarkers\src\View\AjaxView.php
    C:\Users\Pamela\bookmarkers\src\View\AppView.php
    C:\Users\Pamela\bookmarkers\src\View\Helper
    C:\Users\Pamela\bookmarkers\src\View\Helper\empty

2 个答案:

答案 0 :(得分:1)

我相信您还没有完成教程指示您创建文件/bookmarks/tagged的部分。

  

现在,如果您访问tags()网址,CakePHP将显示错误,告知您尚未创建视图文件。接下来,让我们为src/Template/Bookmarks/tags.ctp操作构建视图文件。在<h1> Bookmarks tagged with <?= $this->Text->toList($tags) ?> </h1> <section> <?php foreach ($bookmarks as $bookmark): ?> <article> <!-- Use the HtmlHelper to create a link --> <h4><?= $this->Html->link($bookmark->title, $bookmark->url) ?></h4> <small><?= h($bookmark->url) ?></small> <!-- Use the TextHelper to format text --> <?= $this->Text->autoParagraph($bookmark->description) ?> </article> <?php endforeach; ?> </section> 中添加了以下内容:

CGContextScaleCTM(c, 1, -1);
CGContextTranslateCTM(c, 0, -size.height);

Cookbook 3.x: Bookmark Tutorial: Creating the View

答案 1 :(得分:0)

我不知道我做了什么,但我只是重新做了练习,重新复制了代码并且它有效!看起来我第一次犯了错误。

谢谢,伙计们。