Laravel 动作未定义

2021-12-18 00:00:00 php laravel laravel-5

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

After updating from Laravel 4.2 to 5.0, I am getting the following message in almost every page of my application:

UrlGenerator.php 第 561 行中的 InvalidArgumentException:未定义 Action ArticlesController@create.

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

In my routes.php file I have:

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();

有什么帮助吗?谢谢,伊利亚斯

Any help? Thanks, Ilias

推荐答案

您收到此错误是因为 Laravel 5 默认使用命名空间.官方 Laravel 5 升级指南对迁移控制器做了以下说明:

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.json 文件的 classmap 指令中.接下来,您可以从抽象的 app/Http/Controllers/Controller.php 基类中删除命名空间.验证您迁移的控制器是否扩展了这个基类.

Since we are not going to migrate to full namespacing in this guide, add the app/Http/Controllers directory to the classmap directive of your composer.json file. Next, you can remove the namespace from the abstract app/Http/Controllers/Controller.php base class. Verify that your migrated controllers are extending this base class.

在您的 app/Providers/RouteServiceProvider.php 文件中,将命名空间属性设置为 null.

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

在控制器"下此处列出.

最后一行可能会解决您的问题.

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

相关文章