【问题标题】:Unknown action [PostController@deletePost] using URL::action() in Laravel4在 Laravel4 中使用 URL::action() 的未知操作 [PostController@deletePost]
【发布时间】:2013-09-08 16:52:55
【问题描述】:

我是 Laravel 的初学者。我创建了一个简单的博客。在列出管理员帖子的页面中,我放置了一个 delete 链接,并附有帖子 ID 作为参数。

此链接指向一个名为 deletePost 的操作,只是写了它的声明而已。

每当我访问路线public/admin/post 时,我都会收到此消息:

未知操作 [PostController@deletePost]。

这是我的控制器类:

class PostController extends BaseController {

    public function listPosts(){
        $posts = Post::all();
        return View::make('admin.post.list')->with('posts' , $posts);
    }

    public function addPost(){
        $data = Input::all();
        $rules = array(
            'title' => 'required|min:3',
            'body' => 'required|min:10',
        );
        $validator = Validator::make($data, $rules);

        if($validator->passes()){
            $post = new Post();
            $post->title = htmlentities(trim($data['title']));
            $post->body = strip_tags($data['body'], '<strong><pre>');
            $post->save();
            return View::make('admin.post.add')->with('message' , 'Post successfuly added.');
        } else {
            return Redirect::to('admin/post/add')->withErrors($validator);
        }
    }

    public function deletePost($id){
        return $id;
    }

}

还有我的路线:

Route::group(array('prefix' => 'admin'), function(){
    Route::get('/' , function(){
        return View::make('admin.main')->with('title', 'Main');
    });

    Route::group(array('prefix' => 'post'), function(){
        Route::get('/', "PostController@listPosts");
        Route::get('add', function(){ return View::make('admin.post.add'); });
        Route::post('add', "PostController@addPost");
    });
});

最后是产生这个错误的视图:

@extends('layout.layout')

@section('header')

@stop

@section('content')
<h2>Main - Admin - Post Main menu</h2>
<ul>
    <li><a href="{{ url('admin/post/add') }}">Add</a></li>        
</ul>

@if(isset($posts))
    @foreach($posts as $post)
        <p>{{ $post->body }}</p>
        <a href="{{ action('PostController@deletePost', array('id' => $post->id)) }}">Delete</a>
    @endforeach
@endif

<a href="{{ url('admin/') }}">Back</a>
@stop

【问题讨论】:

  • 您没有为 deletePost 操作创建路由

标签: php templates laravel laravel-4


【解决方案1】:

看起来您需要为 deletePost 操作设置路由。假设您的 url 是 admin/post/delete/$id,请尝试在 routes.php 中将其添加为您的帖子组的新行:

Route::get('delete/{any}', "PostController@deletePost");

您可以在视图中使用 link_to_action() 助手来构建整个锚点,包括 HTML 标记/属性/等,而不是使用 {{ action('PostController@deletePost', array('id' =&gt; $post-&gt;id)) }} 构建您的 URL:

{{ link_to_action('PostController@deletePost', 'Delete', $parameters = array($post-&gt;id), $attributes = array()) }}

【讨论】:

  • 非常好,感谢link_to_action 的提示,非常方便。
猜你喜欢
  • 2013-11-25
  • 1970-01-01
  • 1970-01-01
  • 2018-04-13
  • 2016-08-12
  • 1970-01-01
  • 2017-01-11
  • 1970-01-01
  • 2011-09-21
相关资源
最近更新 更多