在Laravel4中使用URL :: action()的未知操作[PostController @ deletePost]

时间:2013-09-05 01:38:41

标签: php templates laravel laravel-4

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

这个链接是一个名为deletePost的动作,只是写了它的声明。

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

  

未知动作[PostController @ deletePost]。

这是我的Controller类:

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

1 个答案:

答案 0 :(得分:1)

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

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

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

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