How to change request method in Yii2 custom URL rules from GET to POST and others?

pandeydip

I am trying to work in RESTfull web services in Yii2 using default controller. But the problem I faced is, I cannot send POST request with parameters. Below is my code:

Url Manager rule in web.php

'urlManager' => [
        'class' => 'yii\web\UrlManager',
        // Disable index.php
        'showScriptName' => false,
        // Disable r= routes
        'enablePrettyUrl' => true,
        'rules' => array(
            ['pattern' => 'api/v1/auth/payment/<id:\d+>', 'route' => 'api/v1/auth/payment'],
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        ),
    ],

AuthController.php file this is inside controller/api/v1/

namespace app\controllers\api\v1;

use app\controllers\api\v1\components\ApiFunctions;
use Yii;
use yii\web\Controller;


class AuthController extends Controller
{
    public function actionPayment()
    {
        $id = Yii::$app->getRequest()->getQueryParam('id');
        json_encode($id);
    }

}

But when I send GET request to http://{url}//api/v1/auth/payment/5 I get response as 5. But I want to get that result when sending POST or any other methods.

So how can I achieve that?

tnchalise

Let me show you how i solve it for my application.

A simple application structure i have constructed for basic application setup was.

------ app

------modules

----------api

-------------modules

----------------v1

-------------------controllers

-------------------models

-------------------etc

You can simply go through the application setup first.

Then define verb filtering in every controller or sort it out by defining in a common class, as i have done here.

Then with the same rule you have defined in urlManager, you will able be to get the request query parameter.

Hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Yii2 URL manager rules and forms with GET method

From Dev

Yii2: data-method='post' is sending GET request

From Dev

Custom URL rules with modules in Yii2

From Dev

how to get post value from multiple array in yii2

From Dev

Yii2 request parameter from url

From Dev

how to change flask default http method from GET to POST

From Dev

how to config yii2 urlManager rules with aliases and $_GET parameter

From Dev

How to validate params comming from GET request in Yii2 RESTful service

From Dev

yii2:- how can i manage angularjs post request in yii2

From Dev

yii2:- how can i manage angularjs post request in yii2

From Dev

Getting the url where the request is redirected from in yii2?

From Dev

Yii2: call custom method in class User from view

From Dev

Get values through post method from URL

From Dev

Allow to get POST data from outter server YII2

From Dev

Allow to get POST data from outter server YII2

From Dev

How to change access controller redirect/login URL in Yii2?

From Dev

How to change url parameter like directory in Yii2?

From Dev

Yii2 : How to change URL in Action Column Grid View?

From Dev

How to change access controller redirect/login URL in Yii2?

From Dev

How to get clean urls in yii2 like ``post/100``

From Dev

How are get/post requests santized in Yii2?

From Dev

How to send data from django template to views method in post request or in a way data is not visible in the URL

From Dev

URL routing rules in Yii2

From Dev

Yii2 Dynamic URL Routing Rules

From Dev

Translate url rules in Yii2

From Dev

Translate url rules in Yii2

From Dev

Yii2 Dynamic URL Routing Rules

From Dev

Yii2 get controller/action from url

From Dev

How to get JSON from URL with Get Request?

Related Related

  1. 1

    Yii2 URL manager rules and forms with GET method

  2. 2

    Yii2: data-method='post' is sending GET request

  3. 3

    Custom URL rules with modules in Yii2

  4. 4

    how to get post value from multiple array in yii2

  5. 5

    Yii2 request parameter from url

  6. 6

    how to change flask default http method from GET to POST

  7. 7

    how to config yii2 urlManager rules with aliases and $_GET parameter

  8. 8

    How to validate params comming from GET request in Yii2 RESTful service

  9. 9

    yii2:- how can i manage angularjs post request in yii2

  10. 10

    yii2:- how can i manage angularjs post request in yii2

  11. 11

    Getting the url where the request is redirected from in yii2?

  12. 12

    Yii2: call custom method in class User from view

  13. 13

    Get values through post method from URL

  14. 14

    Allow to get POST data from outter server YII2

  15. 15

    Allow to get POST data from outter server YII2

  16. 16

    How to change access controller redirect/login URL in Yii2?

  17. 17

    How to change url parameter like directory in Yii2?

  18. 18

    Yii2 : How to change URL in Action Column Grid View?

  19. 19

    How to change access controller redirect/login URL in Yii2?

  20. 20

    How to get clean urls in yii2 like ``post/100``

  21. 21

    How are get/post requests santized in Yii2?

  22. 22

    How to send data from django template to views method in post request or in a way data is not visible in the URL

  23. 23

    URL routing rules in Yii2

  24. 24

    Yii2 Dynamic URL Routing Rules

  25. 25

    Translate url rules in Yii2

  26. 26

    Translate url rules in Yii2

  27. 27

    Yii2 Dynamic URL Routing Rules

  28. 28

    Yii2 get controller/action from url

  29. 29

    How to get JSON from URL with Get Request?

HotTag

Archive