Laravel 路由到控制器动作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24583815/
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-09-14 09:46:32  来源:igfitidea点击:

Laravel Routing to controller actions

phplaravellaravel-routing

提问by Daniel Del Core

I've been trying for a while now to get laravel to route to my controller actions. I'm not sure of the best way to do this in the laravel framework.

我已经尝试了一段时间让 laravel 路由到我的控制器操作。我不确定在 laravel 框架中执行此操作的最佳方法。

How im picturing it in my head is like this.

我如何在脑海中想象它是这样的。

I have a route setup to link to my controllers actions. so when i type domain.com/Home/Profile It will call the HomeController's profile action. which will do some processing and display the view on the page. ( like mvc4)

我有一个路由设置来链接到我的控制器操作。所以当我输入 domain.com/Home/Profile 时,它​​会调用 HomeController 的配置文件操作。它将做一些处理并在页面上显示视图。(如 mvc4)

I may be going about this the wrong way but im just trying to avoid making a route for every view. I'm more after the correct method to this problem or whatever laravel devs do as a standard practice because it just feels strange to have a route for every view.

我可能会以错误的方式解决这个问题,但我只是试图避免为每个视图制作一条路线。我更喜欢解决这个问题的正确方法,或者 Laravel 开发人员作为标准做法所做的任何事情,因为为每个视图设置一条路线感觉很奇怪。

So far this is my code: routes.php

到目前为止,这是我的代码:routes.php

Route::get('/', 'HomeController@index');

Route::any('/{controller}/{action?}', function ($controller, $action = 'index') 
{
    $class = $controller.'Controller';
    $controller = new $class()

    return $controller->{$action}();

});

HomeController:

家庭控制器:

class HomeController extends BaseController 
{
    protected $layout = 'layouts.master';


    public function index()
    {
        $this->layout->content = View::make('index');
        return View::make('index');
    }

    public function profile()
    {
        $this->layout->content = View::make('profile');
    }
} 

Thanks for any help in advance.

提前感谢您的任何帮助。

采纳答案by Daniel Del Core

Found the answer that worked for me. It's Restful Controllers

找到了对我有用的答案。这是宁静的控制器

I registered the controller with this line

我用这条线注册了控制器

Route::get('/', 'HomeController@index');
Route::controller('/', 'HomeController');

And changed my controller actions to getProfile()

并将我的控制器操作更改为 getProfile()

回答by hook zou

this code worked for me

这段代码对我有用

$api->any('order_manage/{action?}', function ($action = 'index'){
        $app = app();
        $namespace = 'App\Http\Controllers';
        $controller = $app->make($namespace.'\ProjectOrderController');
        return $controller->callAction($action,[request()]);
    });

回答by Wallaaa