从Laravel 4.2更新到5.0后,我的应用程序几乎每一页都会收到以下消息:

InvalidArgumentException in UrlGenerator.php line 561: Action ArticlesController@create not defined.

在我的routes.php文件中,我有:

Route::get('articles/create', ['as' => 'articles.create', 'uses' => 'ArticlesController@create']);
Route::post('articles/create', ['as' => 'articles.create.handle', 'uses' => 'ArticlesController@handleCreate']);

在我的控制器中:

class ArticlesController extends Controller {

    public function create()
    {
        $input=null;
        if (Input::old()) {
            $input = Input::old();
        }
        $tagsJson = Tag::all()->toJson();
        $categories = ArticleCategory::all();
        return View::make('admin.articles.create', compact(array('tagsJson', 'categories', 'input')));
    }

    public function handleCreate()
    {
        $input = Input::all();

        if ($input['op']=="preview") {
            return redirect()->action('ArticlesController@create')->withInput();
        } else if ($input['op']=="post") {
            //
        }

    }
}

The error I get comes from this line:

return redirect()->action('ArticlesController@create')->withInput();

有什么帮助吗?

推荐答案

You are getting this error because Laravel 5 uses namespacing by default. The official Laravel 5 upgrade guide says the following about migrating your controllers:

因为在本指南中我们不打算迁移到完整的名称空间,所以将app/Http/Controllers目录添加到composer的classmap指令中.json文件.接下来,可以从抽象app/Http/Controllers/Controller中删除名称空间.php基类.验证迁移的控制器是否正在扩展此基类.

In your app/Providers/RouteServiceProvider.php file, set the namespace property to null.

Listed here under "controllers".

The last line is probably the one that will solve your issue.

Laravel相关问答推荐

LaravelEloquent 的模型如何将值从控制器内部的函数传递到Render()

在 blade laravel 中以格式编号显示长数字

Laravel:在行的子集上同步多对多

如何从@foreach 循环中捕获所有数据并将其传递给 值? - Laravel

Laravel where 子句只返回一个数据库条目

即使上传了文件,Laravel Input::hasFile('image') 也会返回 false

在 Laravel 5 中添加新配置文件不起作用

磁盘 [视频] 没有配置的驱动程序

PHP Laravel:如何设置或获取会话数据?

Laravel - 验证 - 如果字段为空则需要

Laravel - 排序的集合输出不是数组

在 laravel 6.0 中未定义命令ui

如何更改默认 Laravel Auth 登录视图

我如何从给定的日期时间 struct 创建Carbon 对象?

为什么在 Laravel 的 DB::select 中使用 DB::raw?

如何重定向到laravel上的公共文件夹

判断输入是否来自控制台

在 Laravel 中将 Eloquent 导出到 Excel 时如何包含列标题?

Laravel 检测手机/平板电脑并加载正确的视图

同一模型上的 Laravel 父/子关系