Laravel: Load method in another controller without changing the url

依然范特西╮ 提交于 2019-12-17 17:36:20

问题


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?

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!'
    }

}

回答1:


You can use (L3 only)

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

In L4 you can use

$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

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

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




回答2:


( by neto in Call a controller in Laravel 4 )

Use IoC...

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

Eg:

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

and you may also give params

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



回答3:


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:

// 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 commonFunction in BaseController and how it's used in the two controllers.

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

}



回答4:


if you were in AbcdController and trying to access method public function test() which exists in OtherController you could just do:

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

This should work in L5.1



来源:https://stackoverflow.com/questions/17034616/laravel-load-method-in-another-controller-without-changing-the-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!