You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu16+Nginx环境下ZendFramework3路由无法匹配问题求助

Hey there, let's tackle this ZF3 routing issue you're facing on Ubuntu 16 with Nginx. The homepage loads fine but specific routes throw a "not found" error—super frustrating, I get it. Let's go through the most likely fixes step by step:

1. Fix Nginx URL Rewrite Configuration (Most Common Culprit)

ZF3 is a single-entry application, which means all requests need to be forwarded to public/index.php. If your Nginx config isn't set up to handle this, it'll try to look for physical files/directories for routes like /signup/customer (which don't exist) instead of passing them to ZF3's router.

Update your Nginx server block to point to your project's public directory and enable proper rewrites:

server {
    listen 80;
    server_name your-domain.com; # Replace with your actual domain
    root /path/to/your/zf3-project/public; # Critical: point to the public folder, not project root
    index index.php;

    location / {
        # Forward all non-existent files/dirs to index.php
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        # Ubuntu 16 uses PHP 7.0 by default—adjust socket path if you're using a different version
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
}

After saving the config, restart Nginx to apply changes:

sudo systemctl restart nginx
2. Verify DashboardManager Module Routing Configuration

You shared a snippet of your websites route, but the problematic routes (/signup/customer, /auth/login, etc.) need to be explicitly defined in your module's router config. Double-check that these routes exist in module/DashboardManager/config/module.config.php:

For example, a proper /signup route setup would look like this:

return array(
    'router' => array(
        'routes' => array(
            // Your existing 'websites' route...
            'signup' => array(
                'type' => 'Segment',
                'options' => array(
                    'route' => '/signup[/:action]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                    'defaults' => array(
                        'controller' => 'DashboardManager\Controller\Signup',
                        'action' => 'index',
                    ),
                ),
                // Add child routes if needed for /signup/customer specifically
                'may_terminate' => true,
                'child_routes' => array(
                    'customer' => array(
                        'type' => 'Literal',
                        'options' => array(
                            'route' => '/customer',
                            'defaults' => array(
                                'action' => 'customer',
                            ),
                        ),
                    ),
                ),
            ),
            // Repeat similar setup for 'auth', 'company', 'legal' routes
        ),
    ),
    // Other module config...
);

Also, confirm your module is registered in the main app config (config/application.config.php):

return array(
    'modules' => array(
        'Application',
        'DashboardManager', // This must be present
    ),
    // Other config...
);
3. Enable Debugging to See Detailed Routing Errors

Turn on ZF3's debug mode to get more context about why routes aren't matching. Edit config/autoload/local.php (create it if it doesn't exist) to enable error display:

return array(
    'debug' => true,
    'phpSettings' => array(
        'display_errors' => true,
        'error_reporting' => E_ALL,
    ),
);

Now when you hit a broken route, you'll see specific errors—like a missing controller action, incorrect namespace, or route mismatch—that will point you directly to the problem.

4. Check Controller Class and Namespace Correctness

Make sure the controllers for your routes exist and are properly namespaced. For example, the /signup/customer route points to DashboardManager\Controller\SignupController::customerAction(), so:

  • The file must exist at module/DashboardManager/src/Controller/SignupController.php
  • The class must have the correct namespace and extend AbstractActionController:
namespace DashboardManager\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class SignupController extends AbstractActionController
{
    public function customerAction()
    {
        // Your action logic here
        return new ViewModel();
    }
}
5. Watch for Case Sensitivity Issues

ZF3's router is case-sensitive by default. If your links use uppercase letters (e.g., /Signup/Customer) but your route config uses lowercase (/signup/customer), the match will fail. Ensure your links and route definitions use consistent casing.

Start with the Nginx rewrite fix first—it's the most common reason for this exact symptom. If that doesn't work, move on to verifying your route configs and enabling debugging.

内容的提问来源于stack exchange,提问作者Ann S.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:48:36