动态创建Laravel Request对象

时间:2016-10-26 12:57:37

标签: php request laravel-5.1

我在一个控制器中处理数据并希望将其进一步传递到另一个控制器以避免重复代码。

有没有办法设置其他控制器Mathews-MacBook-Pro:susu mathewmorse$ phonegap build android [phonegap] executing 'cordova build android ' ... cp: no such file or directory: /Users/mathewmorse/Documents/www/susu/www/www/res/icon/android/drawable-ldpi-icon.png cp: no such file or directory: /Users/mathewmorse/Documents/www/susu/www/www/res/icon/android/drawable-mdpi-icon.png cp: no such file or directory: /Users/mathewmorse/Documents/www/susu/www/www/res/icon/android/drawable-hdpi-icon.png cp: no such file or directory: /Users/mathewmorse/Documents/www/susu/www/www/res/icon/android/drawable-xhdpi-icon.png cp: no such file or directory: /Users/mathewmorse/Documents/www/susu/www/www/res/icon/android/drawable-xxhdpi-icon.png cp: no such file or directory: /Users/mathewmorse/Documents/www/susu/www/www/res/icon/android/drawable-xxxhdpi-icon.png cp: no such file or directory: /Users/mathewmorse/Documents/www/susu/www/www/res/screen/android/drawable-land-ldpi-screen.png cp: no such file or directory: /Users/mathewmorse/Documents/www/susu/www/www/res/screen/android/drawable-land-mdpi-screen.png cp: no such file or directory: /Users/mathewmorse/Documents/www/susu/www/www/res/screen/android/drawable-land-hdpi-screen.png cp: no such file or directory: /Users/mathewmorse/Documents/www/susu/www/www/res/screen/android/drawable-land-xhdpi-screen.png cp: no such file or directory: /Users/mathewmorse/Documents/www/susu/www/www/res/screen/android/drawable-land-xxhdpi-screen.png cp: no such file or directory: /Users/mathewmorse/Documents/www/susu/www/www/res/screen/android/drawable-land-xxxhdpi-screen.png cp: no such file or directory: /Users/mathewmorse/Documents/www/susu/www/www/res/screen/android/drawable-port-ldpi-screen.png cp: no such file or directory: /Users/mathewmorse/Documents/www/susu/www/www/res/screen/android/drawable-port-mdpi-screen.png cp: no such file or directory: /Users/mathewmorse/Documents/www/susu/www/www/res/screen/android/drawable-port-hdpi-screen.png cp: no such file or directory: /Users/mathewmorse/Documents/www/susu/www/www/res/screen/android/drawable-port-xhdpi-screen.png cp: no such file or directory: /Users/mathewmorse/Documents/www/susu/www/www/res/screen/android/drawable-port-xxhdpi-screen.png cp: no such file or directory: /Users/mathewmorse/Documents/www/susu/www/www/res/screen/android/drawable-port-xxxhdpi-screen.png ANDROID_HOME=/Users/mathewmorse/Library/Android/sdk JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_73.jdk/Contents/Home :preBuild UP-TO-DATE :preDebugBuild UP-TO-DATE :checkDebugManifest :CordovaLib:preBuild UP-TO-DATE :CordovaLib:preDebugBuild UP-TO-DATE :CordovaLib:compileDebugNdk UP-TO-DATE :CordovaLib:compileLint :CordovaLib:copyDebugLint UP-TO-DATE :CordovaLib:mergeDebugProguardFiles UP-TO-DATE :CordovaLib:packageDebugRenderscript UP-TO-DATE :CordovaLib:checkDebugManifest :CordovaLib:prepareDebugDependencies :CordovaLib:compileDebugRenderscript UP-TO-DATE :CordovaLib:generateDebugResValues UP-TO-DATE :CordovaLib:generateDebugResources UP-TO-DATE :CordovaLib:packageDebugResources UP-TO-DATE :CordovaLib:compileDebugAidl UP-TO-DATE :CordovaLib:generateDebugBuildConfig UP-TO-DATE :CordovaLib:generateDebugAssets UP-TO-DATE :CordovaLib:mergeDebugAssets UP-TO-DATE :CordovaLib:processDebugManifest UP-TO-DATE :CordovaLib:processDebugResources UP-TO-DATE :CordovaLib:generateDebugSources UP-TO-DATE :CordovaLib:compileDebugJavaWithJavac UP-TO-DATE :CordovaLib:processDebugJavaRes UP-TO-DATE :CordovaLib:transformResourcesWithMergeJavaResForDebug UP-TO-DATE :CordovaLib:transformClassesAndResourcesWithSyncLibJarsForDebug UP-TO-DATE :CordovaLib:mergeDebugJniLibFolders UP-TO-DATE :CordovaLib:transformNative_libsWithMergeJniLibsForDebug UP-TO-DATE :CordovaLib:transformNative_libsWithSyncJniLibsForDebug UP-TO-DATE :CordovaLib:bundleDebug UP-TO-DATE :prepareAndroidCordovaLibUnspecifiedDebugLibrary UP-TO-DATE :preReleaseBuild UP-TO-DATE :CordovaLib:preReleaseBuild UP-TO-DATE :CordovaLib:compileReleaseNdk UP-TO-DATE :CordovaLib:copyReleaseLint UP-TO-DATE :CordovaLib:mergeReleaseProguardFiles UP-TO-DATE :CordovaLib:packageReleaseRenderscript UP-TO-DATE :CordovaLib:checkReleaseManifest :CordovaLib:prepareReleaseDependencies :CordovaLib:compileReleaseRenderscript UP-TO-DATE :CordovaLib:generateReleaseResValues UP-TO-DATE :CordovaLib:generateReleaseResources UP-TO-DATE :CordovaLib:packageReleaseResources UP-TO-DATE :CordovaLib:compileReleaseAidl UP-TO-DATE :CordovaLib:generateReleaseBuildConfig UP-TO-DATE :CordovaLib:generateReleaseAssets UP-TO-DATE :CordovaLib:mergeReleaseAssets UP-TO-DATE :CordovaLib:processReleaseManifest UP-TO-DATE :CordovaLib:processReleaseResources UP-TO-DATE :CordovaLib:generateReleaseSources UP-TO-DATE :CordovaLib:compileReleaseJavaWithJavac UP-TO-DATE :CordovaLib:processReleaseJavaRes UP-TO-DATE :CordovaLib:transformResourcesWithMergeJavaResForRelease UP-TO-DATE :CordovaLib:transformClassesAndResourcesWithSyncLibJarsForRelease UP-TO-DATE :CordovaLib:mergeReleaseJniLibFolders UP-TO-DATE :CordovaLib:transformNative_libsWithMergeJniLibsForRelease UP-TO-DATE :CordovaLib:transformNative_libsWithSyncJniLibsForRelease UP-TO-DATE :CordovaLib:bundleRelease UP-TO-DATE :prepareComAndroidSupportSupportV42311Library UP-TO-DATE :prepareDebugDependencies :compileDebugAidl UP-TO-DATE :compileDebugRenderscript UP-TO-DATE :generateDebugBuildConfig UP-TO-DATE :generateDebugAssets UP-TO-DATE :mergeDebugAssets UP-TO-DATE :generateDebugResValues UP-TO-DATE :generateDebugResources UP-TO-DATE :mergeDebugResources UP-TO-DATE :processDebugManifest UP-TO-DATE :processDebugResources /Users/mathewmorse/Documents/www/susu/platforms/android/build/intermediates/manifests/full/debug/AndroidManifest.xml:24:23-37 : AAPT: No resource found that matches the given name (at 'icon' with value '@drawable/icon'). FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':processDebugResources'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Users/mathewmorse/Library/Android/sdk/build-tools/23.0.2/aapt'' finished with non-zero exit value 1 * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. BUILD FAILED Total time: 13.317 secs Error: Error code 1 for command: /Users/mathewmorse/Documents/www/susu/platforms/android/gradlew with args: cdvBuildDebug,- b,/Users/mathewmorse/Documents/www/susu/platforms/android/build.gradle,- Dorg.gradle.daemon=true,-Pandroid.useDeprecatedNdk=true - 方法所需的Request对象?我已经追踪了Request继承并找到了Symfony的Request对象,该对象的store属性实际上是request,它包含一个方法ParameterBag来添加带有值的参数。

我已经尝试过以下操作,但结果是add

null

我在这个项目上使用Laravel 5.1。

5 个答案:

答案 0 :(得分:38)

您可以使用replace()

$request = new \Illuminate\Http\Request();

$request->replace(['foo' => 'bar']);

dd($request->foo);

或者,为第二个控制器中发生的任何事情创建Job更有意义,并删除ShouldQueue接口以使其同步运行。

希望这有帮助!

答案 1 :(得分:19)

使用$myRequest = new Request();创建请求对象会使用method = 'GET'创建对象。您可以使用$myRequest->getMethod()检查您的请求方法。 由于request属性包含POST请求的数据,因此默认情况下不能使用$myRequest->request->add()。首先,您必须将请求的方法设置为POST:

$myRequest = new \Illuminate\Http\Request();
$myRequest->setMethod('POST');
$myRequest->request->add(['foo' => 'bar']);
dd($request->foo);

顺便使用$myRequest->query->add(),您可以将数据添加到GET请求中。

答案 2 :(得分:2)

要“避免重复代码”,您需要将通用功能抽象为专用类,为其指定适当的助记符名称,围绕它编写一组单元测试,然后在对控制器进行单元测试时在控制器中对其进行模拟。

但是如果您仍然需要发出请求:

use Illuminate\Http\Request;

$request = new Request([
        'name'   => 'unit test',
        'number'  => 123,
    ]);

如果您需要请求的全部功能,则需要添加一些额外的行

$request
            ->setContainer(app())
            ->setRedirector(app(\Illuminate\Routing\Redirector::class))
            ->validateResolved();

答案 3 :(得分:0)

您可以使用这些方法即时添加request参数。

替换

replace function doc

如果您在控制器中,则在

之类的函数参数中传递Request对象
  function createUser(Illuminate\Http\Request $request){
     $request->replace(array_merge(array("new_key1"=>"new_value1","new_key_n"=>"new_value_n"), $request->all()));
}

合并功能

merge function doc

function createUser(Illuminate\Http\Request $request){
     $request->merge(array("new_key1"=>"new_value1","new_key_n"=>"new_value_n"));
}

添加功能

 function createUser(Illuminate\Http\Request $request){
     $request->request->add(array_merge(array("new_key1"=>"new_value1","new_key_n"=>"new_value_n"), $request->all()));
}

注意:在所有函数中,我们都在扩展请求,这意味着先前的参数将保留在那里。您将添加自己的。您可以全部替换。

答案 4 :(得分:0)

您可以克隆现有请求并用新数据填充它:

$request = (clone request())->replace(['foo' => 'bar']);