简体   繁体   中英

Using ZF2 param() plugin as a module (without ZF2 in place)

I have a php script that receives variables via GET method. I can access the variables using $_GET['var'] , but I want to use ZF2's $this->params()->fromQuery('var') construct, without having ZF2 engine in place.

I am interested in 'how'. In case you are interested in 'why (would I want to do this)', it's because I am working on large existing legacy codebase, rewriting it to use ZF2 step by step. Next step is to use params().

I am thus interested in building up the needed code to make the params() plugin work. My thoughts are to write a trait that can be called into my class, where then inside the class I can use $this->params()->fromQuery() .

My current thoughts

trait ParamTrait
{
    public function params(string $param = null, mixed $default = null)
    {
        //magic

        return $params;
    }
}

class X
{
    use ParamTrait;

    function showGet()
    {
        echo $this->params()->fromQuery('var');
    }
}

The magic part is what I am looking for to fill.

params() is just a controller plugin proxy to Zend\\Http\\Request

You can use this anywhere outside of a full ZF2 MVC app by adding zendframework/zend-http to your composer.json

require{
    "zendframework/zend-http": "2.3.0"
}

include the ./vendor/autoload.php in your file

<?php
require 'vendor/autoload.php';

$request = new \Zend\Http\PhpEnvironment\Request();

//post
$post = $request->getPost();

//query
$query = $request->getQuery();

// etc, etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM