使用" Header"重定向时,无法从对象获取属性功能

时间:2018-01-14 20:54:41

标签: php http-redirect

我试图在插入后展示插入的产品。它看起来像是空的。

这是我用来重定向(' Inventario'控制器)

public function insertarAction(){
// insertarProducto returns true or false
    if($this->producto->insertarProducto()) {

        Flash::addMessage('Prenda ingresada correctamente');
        $this->redirect('/inventario/ingreso-correcto');

    } else {
        Flash::addMessage('Error al ingresar prenda', FLASH::WARNING);
        View::renderTemplate('Inventario/nuevo.html',[
                'user' => Auth::getUser(),
                'producto' => $this->producto,
                'controller' => 'inventario'
            ]);
    }
}

这会将我重定向到我需要的HTML,但没有' producto'对象

我在before方法中创建了产品对象,该方法在调用其他方法之前运行。

这就是'重定向'函数(它在我的核心/控制器类上):

public function redirect($url){
        header('Location: http://' . $_SERVER['HTTP_HOST'] . $url, true, 303);
        exit;   
}

这是ingreso-correcto HTML:

<table>
    <tr>
        <th>Codigo</th>
        <td>{{ producto.codigo }}</td>
    </tr>
    <tr>
        <th>Color</th>  
        <td>{{ producto.color }}</td>
    </tr>
    <tr>
        <th>Cantidad en stock</th>
        <td>{{ producto.cantidad }}</td>
    </tr>
    <tr>
        <th>Descripcion de producto</th>
        <td>{{ producto.descripcion }}</td>
    </tr>
    <tr>
        <th>Precio</th>
        <td>{{ producto.precio }}</td>
    </tr>
    <tr>
        <th>Costo</th>
        <td>{{ producto.costo }}</td>
    </tr>
    <tr>
        <th>Descuento</th>
        <td>{{ producto.descuento }}</td>
    </tr>
    <tr>
        <th>Coleccion</th>
        <td>{{ producto.coleccion }}</td>
    </tr>
        <th>Ganancia</th>
        <td>{{ producto.ganancia }}</td>
    </tr>

</table>

在使用producto方法后如何获取redirect对象?

1 个答案:

答案 0 :(得分:2)

您没有写,insertarProducto()函数做了什么,但如果它没有将产品ID写入用户会话,则必须执行此操作,或者您必须在insertarAction()控制器中对其进行编码功能

如果inventario是一种购物篮,您也可以访问它并尝试为当前用户检索上次添加的产品。

另一种解决方案

您还可以将producto ID附加到您的重定向网址,但它不像使用会话一样干净。

$this->redirect('/inventario/ingreso-correcto/{producto.id}');