php Laravel 动作未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29822302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:36:49  来源:igfitidea点击:

Laravel action not defined

phplaravellaravel-5

提问by Ilias Kouroudis

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

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

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

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

In my routes.php file I have:

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

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

And in my controller:

在我的控制器中:

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

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

采纳答案by Maarten00

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:

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

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.

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

由于我们不会在本指南中迁移到完整命名空间,因此将 app/Http/Controllers 目录添加到 composer.json 文件的 classmap 指令中。接下来,您可以从抽象 app/Http/Controllers/Controller.php 基类中删除命名空间。验证您迁移的控制器是否扩展了这个基类。

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

Listed hereunder "controllers".

上市这里下的“控制器”。

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

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

回答by Hos Mercury

you have to define routes for any URL generated and if you are pointing a controller method from links or form. actions links to methods depends on Routes ..

你必须为任何生成的 URL 定义路由,如果你从链接或表单指向控制器方法。方法的操作链接取决于 Routes ..

When you create a route for this , it will work and the error will disappear .

当您为此创建路由时,它将起作用并且错误将消失。

回答by Ryan

I was getting the same error message, and it was because I'd completely forgotten that I needed to add this to routes/web.php:

我收到了相同的错误消息,这是因为我完全忘记了我需要将其添加到routes/web.php

Route::get('myUrlPath', 'HomeController@myActionName');

Route::get('myUrlPath', 'HomeController@myActionName');

I had been trying to do this from another controller action:

我一直试图从另一个控制器操作中做到这一点:

return redirect()->action('HomeController@myActionName')->with('windowTitle', 'Error. Please contact us.')->with('message', $message);

return redirect()->action('HomeController@myActionName')->with('windowTitle', 'Error. Please contact us.')->with('message', $message);