Laravel 5:控制器相互通信和实例化
Laravel 5: Controller Inter-Communication and Instantiation
过去,我使用了一种专有框架,它有点遵循 Objective-C 类似视图控制器的方案。
基本上我能够在一个控制器中实例化另一个控制器并将一些值传递给它,例如产品数组,然后将对控制器的引用注入我的视图并在我需要时通过发出以下命令呈现它:$controllerReference->render();
这在很多情况下都很有用,例如。如果我有一个负责呈现目录的控制器,我会向它传递一个包含我想看到的所有项目的数组,它会进行分页并自行显示项目...
示例:
在\UI\Homepage\Controller.php(负责首页的负责人):
// Instantiate a ProductDisplay Controller
$controllRef = new \UI\ProductDisplay\Controller();
$controllRef->setProducts( ... ); // Inject the product array
// Load the current view for this controller and pass it a reference for the ProductDisplay controller
$this->loadSelfView(["controllRef" => $controllRef]);
在\UI\Homepage\View.php(之前加载的视图):
// some html of the view
$controllRef->render(); // Render the ProductDisplay view here!
// some other html
如何在 Laravel 中实现此功能?根据我一直在阅读的内容 Laravel 试图避免这种行为,为什么?解决方法是什么?
谢谢。
这是我将如何做到这一点,只有当被调用的控制器方法 return 像 return view('home');
这样的视图对象时它才有效:
// Get the controller class from the IOC container
$myController= \App::make(MyController::class);
// Execute the controller method (which return a View object)
$view = $myController->myControllerMethod($someParams);
// Return the View html content
$html = $view->render();
您可以使用由所有其他控制器扩展的 Controller.php class 在其中创建通用方法:
- 获取控制器实例
- 使用 x 个参数调用正确的方法
- Return 呈现的内容(如果是视图)class 或抛出异常(例如)
在 Laravel 的最新版本中,可以使用 Blade 的 @inject
指令。它的主要目的似乎是注入服务,但我成功注入了控制器操作。
以下是您必须执行的操作的片段:
@inject('productController', 'App\Http\Controllers\ProductController')
{!! $productController->index() !!}
请记住:由于您是直接调用控制器方法,而不是通过路由器,因此您必须传递所有必需的参数。如果该操作需要一个请求实例,您可以这样称呼它:
{!! $productController->index(request()) !!}
可能调用的操作返回的视图包含 HTML 代码。将方法调用包装在 {!!
和 !!}
中很重要。使用常规 {{ }}
标签将导致 HTML 代码被模板引擎转义。
过去,我使用了一种专有框架,它有点遵循 Objective-C 类似视图控制器的方案。
基本上我能够在一个控制器中实例化另一个控制器并将一些值传递给它,例如产品数组,然后将对控制器的引用注入我的视图并在我需要时通过发出以下命令呈现它:$controllerReference->render();
这在很多情况下都很有用,例如。如果我有一个负责呈现目录的控制器,我会向它传递一个包含我想看到的所有项目的数组,它会进行分页并自行显示项目...
示例:
在\UI\Homepage\Controller.php(负责首页的负责人):
// Instantiate a ProductDisplay Controller
$controllRef = new \UI\ProductDisplay\Controller();
$controllRef->setProducts( ... ); // Inject the product array
// Load the current view for this controller and pass it a reference for the ProductDisplay controller
$this->loadSelfView(["controllRef" => $controllRef]);
在\UI\Homepage\View.php(之前加载的视图):
// some html of the view
$controllRef->render(); // Render the ProductDisplay view here!
// some other html
如何在 Laravel 中实现此功能?根据我一直在阅读的内容 Laravel 试图避免这种行为,为什么?解决方法是什么?
谢谢。
这是我将如何做到这一点,只有当被调用的控制器方法 return 像 return view('home');
这样的视图对象时它才有效:
// Get the controller class from the IOC container
$myController= \App::make(MyController::class);
// Execute the controller method (which return a View object)
$view = $myController->myControllerMethod($someParams);
// Return the View html content
$html = $view->render();
您可以使用由所有其他控制器扩展的 Controller.php class 在其中创建通用方法:
- 获取控制器实例
- 使用 x 个参数调用正确的方法
- Return 呈现的内容(如果是视图)class 或抛出异常(例如)
在 Laravel 的最新版本中,可以使用 Blade 的 @inject
指令。它的主要目的似乎是注入服务,但我成功注入了控制器操作。
以下是您必须执行的操作的片段:
@inject('productController', 'App\Http\Controllers\ProductController')
{!! $productController->index() !!}
请记住:由于您是直接调用控制器方法,而不是通过路由器,因此您必须传递所有必需的参数。如果该操作需要一个请求实例,您可以这样称呼它:
{!! $productController->index(request()) !!}
可能调用的操作返回的视图包含 HTML 代码。将方法调用包装在 {!!
和 !!}
中很重要。使用常规 {{ }}
标签将导致 HTML 代码被模板引擎转义。