有沒有神奇的,但你可以很容易地在3個步驟覆蓋ViewService:
1,創建視圖工廠(your_project_path/app/MyViewFactory.php
)
<?php
/**
* Created by PhpStorm.
* User: panos
* Date: 5/2/15
* Time: 1:35 AM
*/
namespace App;
use Illuminate\View\Factory;
class MyViewFactory extends Factory {
public function make($view, $data = array(), $mergeData = array())
{
if (\Request::ajax()) {
return $data;
}
return parent::make($view, $data, $mergeData);
}
}
2.創建視圖服務提供商(your_project_path/app/providers/MyViewProvider.php
)
<?php namespace App\Providers;
use App\MyViewFactory;
use Illuminate\View\ViewServiceProvider;
class MyViewProvider extends ViewServiceProvider {
/**
* Register the application services.
*
* @return void
*/
public function register()
{
parent::register();
}
/**
* Overwrite original so we can register MyViewFactory
*
* @return void
*/
public function registerFactory()
{
$this->app->singleton('view', function($app)
{
// Next we need to grab the engine resolver instance that will be used by the
// environment. The resolver will be used by an environment to get each of
// the various engine implementations such as plain PHP or Blade engine.
$resolver = $app['view.engine.resolver'];
$finder = $app['view.finder'];
// IMPORTANT in next line you should use your ViewFactory
$env = new MyViewFactory($resolver, $finder, $app['events']);
// We will also set the container instance on this view environment since the
// view composers may be classes registered in the container, which allows
// for great testable, flexible composers for the application developer.
$env->setContainer($app);
$env->share('app', $app);
return $env;
});
}
}
3.in your_project_path/config/app.php
: 變化'Illuminate\View\ViewServiceProvider',
到'App\Providers\MyViewProvider',
什麼該做:
它告訴你的應用程序使用另一個將在MyViewProvider.php
33線註冊您的視圖工廠 $env = new MyViewFactory($resolver, $finder, $app['events']);
收看提供商將檢查是否請求AJAX和返回,如果真或繼續使用原來的行爲 return parent::make($view, $data, $mergeData);
在MyViewFactory.php
線19
希望這有助於你,
恕我直言,你要實現的目標也不是沒有可能接觸到控制器的方法。我建議你使用@fungku的方法並調整你需要的方法。至少你有一個地方,然後再次改變你的控制器的響應行爲。 –