yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.9k forks source link

redirect() is not working as expected #13430

Closed tsanchev closed 7 years ago

tsanchev commented 7 years ago

What steps will reproduce the problem?

i have a module with normal CRUD actions after last composer update the redirect in create and update to view are no longer working as expected

What is the expected result?

it should be /module_name/default/view?id=1

What do you get instead?

i get redirected /view?id=1

Additional info

Q A
Yii version 2.0.11-dev
PHP version
Operating system
tsanchev commented 7 years ago

this is the code from redirect that is causing the porblem if (is_array($url) && isset($url[0])) { // ensure the route is absolute $url[0] = '/' . ltrim($url[0], '/'); }

as i'm passing to redirect(['view', 'id' => 1); it converts it to ['/view', 'id' =>1]

then the following call to Url::to( ['/view', 'id' =>1] ) retruns /view?id=1

if i comment ` // if (is_array($url) && isset($url[0])) { // ensure the route is absolute // $url[0] = '/' . ltrim($url[0], '/'); // }

// if (strpos($url, '/') === 0 && strpos($url, '//') !== 0) { // $url = Yii::$app->getRequest()->getHostInfo() . $url; // } `

everithing works as expected

yii-bot commented 7 years ago

Thanks for posting in our issue tracker. In order to properly assist you, we need additional information:

Thanks!

This is an automated comment, triggered by adding the label status:need more info.

samdark commented 7 years ago

@tsanchev it's not clear on how to reproduce it. Could you elaborate what "normal CRUD actions" are?

tsanchev commented 7 years ago

@samdark By "normal CRUD actions" i mean, i have generated model and CRUD using gii module, using latest gii version from several months ago. i'm using advanced template. the problem occurs in the backend of the project. i have lots of modules with models in them and CRUDs (all of them generated with gii) and after updating the project with composer yesterday, none of the generated code by gii involving redirects is working properly.

for example this is code for update from DefaultController.php from module 'products' which is in the backend

`

/**
 * Updates an existing Products model.
 * If update is successful, the browser will be redirected to the 'view' page.
 * @param string $id
 * @return mixed
 */
public function actionUpdate($id)
{
    $model = $this->findModel($id);

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->product_id]);
    } else {
        return $this->render('update', [
            'model' => $model,
        ]);
    }
}

`

on post after model get saved, it redirects me to http://localhost/myproject/backend/view?id=X instead of http://localhost/myproject/backend/products/default/view?id=X

the redirect acts as it is redirect(['/view' .... and not redirect(['view'

this peace of code i mentioned in the previous post :

`

// if (is_array($url) && isset($url[0])) { // ensure the route is absolute // $url[0] = '/' . ltrim($url[0], '/'); // }

// if (strpos($url, '/') === 0 && strpos($url, '//') !== 0) { // $url = Yii::$app->getRequest()->getHostInfo() . $url; // }

`

is from Yii itself from the implementation of redirect() method

teruk commented 7 years ago

I experience the same problem with the same behavoir and no redirect is working probably if only the action is given. I'm also using the advanced template but with no modules so far. There is no difference between redirect with or without additional parameters. I did not use the Gii to generate the actions.

The problem occurred today after an composer update, but strangely nothing is shown in the logs. Still need to investigate that a little more. Some changes in the last 5 days must have caused the problem because that was the last time before today that I ran composer update and it worked fine until today.

With Yii version 2.0.10 everything works as aspected

rob006 commented 7 years ago

Probably #13393 caused this bug.

samdark commented 7 years ago

@teruk can you revert that change and try it w/o it? Will be able to do it evening or tomorrow myself...

teruk commented 7 years ago

@samdark I changed my version to dev-master again and I can confirm, that this change caused the problem.

samdark commented 7 years ago

Reverted https://github.com/yiisoft/yii2/commit/eab3489ec711b1ad9de0158e402fffaba999f4ff. Thanks for checking.