php Laravel:在另一个控制器中加载方法而不更改 url

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

Laravel: Load method in another controller without changing the url

phplaravelcontroller

提问by enchance

I have this route: Route::controller('/', 'PearsController');Is it possible in Laravel to get the PearsController to load a method from another controller so the URL doesn't change?

我有这条路线:Route::controller('/', 'PearsController');在 Laravel 中是否有可能让 PearsController 从另一个控制器加载一个方法,这样 URL 就不会改变?

For example:

例如:

// route:
Route::controller('/', 'PearsController');


// controllers
class PearsController extends BaseController {

    public function getAbc() {
        // How do I load ApplesController@getSomething so I can split up
        // my methods without changing the url? (retains domain.com/abc)
    }

}

class ApplesController extends BaseController {

    public function getSomething() {
        echo 'It works!'
    }

}

回答by The Alpha

You can use (L3 only)

您可以使用(仅限 L3)

Controller::call('ApplesController@getSomething');

In L4you can use

L4你可以使用

$request = Request::create('/apples', 'GET', array());
return Route::dispatch($request)->getContent();

In this case, you have to define a route for ApplesController, something like this

在这种情况下,您必须为 定义一个路由ApplesController,就像这样

Route::get('/apples', 'ApplesController@getSomething'); // in routes.php

In the array()you can pass arguments if required.

array()如果需要,您可以在中传递参数。

回答by Joeri

( by netoin Call a controller in Laravel 4)

(由neto在 Laravel 4调用控制器

Use IoC...

使用国际奥委会...

App::make($controller)->{$action}();

Eg:

例如:

App::make('HomeController')->getIndex();

and you may also give params

你也可以给参数

App::make('HomeController')->getIndex($params);

回答by Antonio Carlos Ribeiro

You should not. In MVC, controllers should not 'talk' to each other, if they have to share 'data' they should do it using a model, wich is the type of class responsible for data sharing in your app. Look:

你不应该。在 MVC 中,控制器不应该相互“交谈”,如果他们必须共享“数据”,他们应该使用模型来完成,这是负责应用程序中数据共享的类的类型。看:

// route:
Route::controller('/', 'PearsController');


// controllers
class PearsController extends BaseController {

    public function getAbc() 
    {
        $something = new MySomethingModel;

        $this->commonFunction();

        echo $something->getSomething();
    }

}

class ApplesController extends BaseController {

    public function showSomething() 
    {
        $something = new MySomethingModel;

        $this->commonFunction();

        echo $something->getSomething();
    }

}

class MySomethingModel {

    public function getSomething() 
    {
        return 'It works!';
    }

}

EDIT

编辑

What you can do instead is to use BaseController to create common functions to be shared by all your controllers. Take a look at commonFunctionin BaseControllerand how it's used in the two controllers.

您可以做的是使用 BaseController 创建由所有控制器共享的通用功能。看看commonFunctioninBaseController以及它是如何在两个控制器中使用的。

abstract class BaseController extends Controller {

    public function commonFunction() 
    {
       // will do common things 
    }

}

class PearsController extends BaseController {

    public function getAbc() 
    {
        return $this->commonFunction();
    }

}

class ApplesController extends BaseController {

    public function showSomething() 
    {
        return $this->commonFunction();
    }

}

回答by Kal

if you were in AbcdControllerand trying to access method public function test()which exists in OtherControlleryou could just do:

如果您在AbcdController并尝试访问public function test()存在于OtherController您中的方法,则可以执行以下操作:

$getTests = (new OtherController)->test();

This should work in L5.1

这应该适用于 L5.1