ZF2返回$ this-> redirect() - > toRoute(...)在Web服务器上不起作用,仅适用于Localhost

时间:2014-08-25 17:57:39

标签: php .htaccess redirect zend-framework2 webserver

我一直在努力解决这个问题一个多月了,似乎没有任何帮助:(((( 这就是我决定在这里发帖的原因。

我有一个ZF2应用程序,如下所示: (在网络托管上) root(www.exam.example.com - 子域指向/ public) 所以:

root
   config/
   module/
      Administration/
         config/
            module.config.php
         src/
         view/
         Module.php
   public/
      css/
      js/
      img/
      .htaccess(which has the RewriteEngine On)
      index.php
   vendor/
      ...

问题在于,当我在localhost(相同的doc结构)上时,返回$ this-> redirect() - > toRoute(' products / default' ,数组('动作' =>'创建'))效果很好! 另一方面,当项目上传到Web主机上时,它根本不起作用,它会使页面空白,从不重定向。

我知道这里有一些关于这类问题的帖子,但那些建议对我不起作用。

我认为它可能与主机配置有关,而且子域指向/ public但我不知道。

以下是一些细节:

我的module.config.php文件:(路由器)

<?php

namespace Administration;

return array(
    'controllers' => array(
        'invokables' => array(
            'Administration\Controller\Products' => 'Administration\Controller\ProductsController',
        ),
    ),
    'router' => array(
        'routes' => array(
            'products' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/products',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Administration\Controller',
                        'controller'    => 'Products',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/[:action][/:id]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(
                            '__NAMESPACE__' => 'Administration\Controller',
                            'controller'    => 'Products',
                            'action'        => 'index',
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),

我的控制器:( ProductsController.php(只是createAction))

    class ProductsController extends AbstractActionController
{

    public function createAction()
    {
        // Getting the ID(Type of the Product to create)!!!
        $productType = (int)$this->params()->fromRoute('id'); 

        if($productType > 0 && $productType < 4) {
            // Defining the Form depending on what type is the desired product...
            $form = $this->defineForm($productType);
            // Instantiate the Product with a type of whatever it is
            $product = new Product($productType);

            // Getting the type of the request - POST or GET
            $request = $this->getRequest(); 
            // Getting the service manager to have access to the configuration
            $sm = $this->getServiceLocator(); 
            // Getting the table which can interact with DB
            $table = $this->getProductsTable(); 
            $imgTable = $this->getImagesTable();

            if($request->isPost())
            {
                // Getting the post request data
                $postRequestData = $request->getPost()->toArray(); 
                // Getting the uploaded files
                $postRequestUploadedFiles = $request->getFiles()->toArray(); 
                // Merging the files uploaded with the rest of the data.
                $mergedData = array_merge($postRequestData, $postRequestUploadedFiles); 
                // Setting the input filter to filter the form fields.
                $form->setInputFilter($product->getInputFilter());
                // Filling in the post request data into the form to check then if it is valid depending on the input.
                $form->setData($postRequestData);

                // Checking form validity...
                if($form->isValid())
                {
                    $data = $form->getData();
                    $adapter = $this->getHttpAdapter();

                    $product->exchangeArray($data);
                    $date = date('Y-m-d');
                    $product->setDatePublished($date);
                    $savedProduct = $table->saveProduct($product);
                    if($savedProduct)
                    {
                        $imagesArray = array();
                        foreach ($postRequestUploadedFiles['images'] as $image)
                        {
                            if(!empty($image['tmp_name']) && substr($image['type'],0,5) == 'image')
                            {
                                $name = $image['name'];
                                $imageData = file_get_contents($image['tmp_name']);
                                $imageType = $image['type'];
                                $img = new Image($name, $imageData, $imageType);
                                $img->setRelation($savedProduct['id']);
                                array_push($imagesArray, $img);
                            }
                        }
                        $imgTable->saveImages($imagesArray);
                        return $this->redirect()->toRoute('products');
                    }
                    else 
                    {
                        return $this->redirect()->toRoute('products/default', array('action' => 'create'));
                    }
                }
            }

            return new ViewModel(array('form' => $form, 'type' => $productType));
        } 
        else {
            return $this->redirect()->toRoute('products');
        }
    }
}

任何帮助都会得到很高的评价!!!

谢谢

修改 我设法解决了这个问题。我不知道它为什么会发生,但显然核心框架文件中缺少某些内容。重新安装核心后,它开始工作:))) 如果有人有这个问题,你可以用我的方式处理它:D

1 个答案:

答案 0 :(得分:0)

使用以下命令在public / index.php上打开error_reporting: 使用error_reporting(E_ALL); 在页面的开头.. 并再次检查您的问题,您可能会得到一些帮助信息。