在定义时,Action CourseController @ publish未定义错误

时间:2016-04-13 12:19:31

标签: php laravel

我为资源控制器定义了一个自定义函数,如下所示:

public function publish (Request $request, $post_id = NULL)
    {
        if (!is_null($post_id)) {
            $result = $this->update($request, $post_id);
        } else {
            $result = $this->store($request);
        }

        if ($result['success']) {
            return ['success' => true, 'msg' => $result['msg']];
        } else {
            return ['success' => false, 'msg' => $result['msg']];
        }
    }

在刀片模板中,我想像这样链接到它:

$('#publishBtn').click(function () {
   $.post('{{action('CourseController@publish')}}', $('#createCourseForm').serialize() + '&post_status=published', function (data) {
    data.success ? location.href = '/admin/course' : showMsg(data.msg, 'danger');
   });
 });

但是在显示下面的打开页面错误后会显示该方法:

    Action App\Http\Controllers\CourseController@publish not defined.   
(View: D:\wamp\www\lms\resources\views\admin\pages\course\course-create.blade.php)

2 个答案:

答案 0 :(得分:1)

您还需要为此创建路线。

答案 1 :(得分:1)

CourseController资源路由之前,为publish方法

添加路由
Route::post('publish', [
    'uses' => 'CourseController@publish',
    'as'   => 'publish'
]);

....

Route::resource('course', 'CourseController');

然后在你的模板中

$('#publishBtn').click(function () {
   $.post('{{route('publish')}}', $('#createCourseForm').serialize() + '&post_status=published', function (data) {
    data.success ? location.href = '/admin/course' : showMsg(data.msg, 'danger');
   });
 });