在CMS教程中添加文章时Cake 3 PHP错误

时间:2017-12-21 19:06:57

标签: php cakephp-3.0

我遇到了Cake Tutorial for Cake 3的问题,直到我添加了Auth组件才行。

现在,无论我使用哪个用户添加新文章,都会导致错误“无法添加您的文章。”

这就是我的ArticlesController的样子:

<?php


namespace App\Controller;

class ArticlesController extends AppController
{
    //Wyświetla wszystkie artykuły
    public function index()
    {
        $this->loadComponent('Paginator');
        $articles = $this->Paginator->paginate($this->Articles->find());
        $this->set(compact('articles'));
}

//Wyświetla pojedynczy artykuł na podstawie sluga
public function view($slug = null)
{
    $article = $this->Articles->findBySlug($slug)->firstOrFail();
    $this->set(compact('article'));
}

//Dodawanie artykułu
public function add()
{
    $article = $this->Articles->newEntity();
    if ($this->request->is('post')) {
        $article = $this->Articles->patchEntity($article, $this->request->getData());

        $article->user_id = $this->Auth->user('id');

        if ($this->Articles->save($article)) {
            $this->Flash->success(__('Your article has been saved.'));
            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('Unable to add your article.'));
    }
    //Pobiera listę tagów dzięki połączeniu Many to Many w ArticlesTable
    $tags = $this->Articles->Tags->find('list');

    //Ustawia tagi do widoku
    $this->set('tags', $tags);


    $this->set('article', $article);
}

//Edycja artykułu
public function edit($slug)
{
    $article = $this->Articles->findBySlug($slug)->contain('Tags')->firstOrFail();
    if ($this->request->is(['post', 'put'])) {
        $this->Articles->patchEntity($article, $this->request->getData(), [
            // Added: Disable modification of user_id.
            'accessibleFields' => ['user_id' => false]
        ]);
        if ($this->Articles->save($article)) {
            $this->Flash->success(__('Your article has been updated.'));
            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('Unable to update your article.'));
    }
    //Pobiera listę tagów dzięki połączeniu Many to Many w ArticlesTable
    $tags = $this->Articles->Tags->find('list');

    //Ustawia tagi do widoku
    $this->set('tags', $tags);


    $this->set('article', $article);
}

//Usuwanie Artykułu
public function delete($slug)
{
    $this->request->allowMethod(['post', 'delete']);

    $article = $this->Articles->findBySlug($slug)->firstOrFail();
    if ($this->Articles->delete($article)) {
        $this->Flash->success(__('The {0} article has been deleted.', $article->title));
        return $this->redirect(['action' => 'index']);
    }
}

//Wyszukiwanie artykułu po tagach
public function tags()
{
    // The 'pass' key is provided by CakePHP and contains all
    // the passed URL path segments in the request.
    $tags = $this->request->getParam('pass');

    // Use the ArticlesTable to find tagged articles.
    $articles = $this->Articles->find('tagged', [
        'tags' => $tags
    ]);

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

public function isAuthorized($user)
{
    $action = $this->request->getParam('action');
    // The add and tags actions are always allowed to logged in users.
    if (in_array($action, ['add', 'tags'])) {
        return true;
    }

    // All other actions require a slug.
    $slug = $this->request->getParam('pass.0');
    if (!$slug) {
        return false;
    }

    // Check that the article belongs to the current user.
    $article = $this->Articles->findBySlug($slug)->first();

    return $article->user_id === $user['id'];
}

public function initialize()
{
    parent::initialize();
    // Allow the tags
    $this->Auth->allow(['tags']);

}   }

我试图不要错过任何步骤,但我仍然不知道它为什么不起作用。编辑很好,它检查用户添加的文章,并允许编辑给他。

任何想法,还是Cake 3 bug?

0 个答案:

没有答案