安装Silex路线,需要帮助

时间:2016-05-27 11:39:57

标签: php routes silex

我注册了以下路线:

$app->register(new HomeServiceProvider());
$app->register(new UserServiceProvider());

$app->mount('/', new HomeControllerProvider());
$app->mount('/', new UserControllerProvider());

两个控制器都有这样的功能:

$controllers->get("/", 'home.controller:index')
    ->bind('homepage');

$controllers->get("/", 'user.controller:collection')
    ->bind('users');

当我转到以下网址时,我可以看到主页:

/Testing/demo/web/index.php

问题:

如何修改它以便系统中的唯一路径是:

/Testing/demo/home
/Testing/demo/user

我已尝试将上述坐标更改为:

$app->mount('/home', new HomeControllerProvider());
$app->mount('/user', new UserControllerProvider());

但我只是得到了404以下内容:

/Testing/demo/web/index.php/home
/Testing/demo/web/index.php/user
/Testing/demo/web/home
/Testing/demo/web/user
/Testing/demo/home
/Testing/demo/user

项目结构:

root
-src
--Home
--User
---UserController.php
---UserControllerProvider.php
---UserModel.php
---UserRepository.php
---UserServideProvider.php
--Application.php
-web
--.htaccess
--index.php

.htaccess文件:

<IfModule mod_rewrite.c>
    Options -MultiViews

    RewriteEngine On
    RewriteBase /src
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [QSA,L]
</IfModule>

我在这里错过了什么吗?

编辑:

// UserController.php
<?php
namespace App\User;

use App\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class UserController
{
    protected $app;

    public function __construct(Application $app)
    {
        $this->app = $app;
    }

    public function collection()
    {
        return $this->app['twig']->render('user/collection.html.twig', [
            'users' => $this->app['user.repository']->collection()
        ]);
    }
}

// UserControllerProvider.php
<?php

namespace App\User;

use Silex\Application;
use Silex\ControllerProviderInterface;

class UserControllerProvider implements ControllerProviderInterface
{
    public function connect(Application $app)
    {
        $controllers = $app['controllers_factory'];
        $controllers->get("/", 'user.controller:collection')
            ->bind('user.collection');

        return $controllers;
    }
}

1 个答案:

答案 0 :(得分:0)

在路线中定义控制器时出错。尝试将其更改为:

$controllers->get("/", '\\App\\User\\UserController::collection')
    ->bind('user.collection');

应用程序在控制器构造函数中不可用。它作为参数传输到应该处理匹配路由的方法。尝试将控制器更改为:

class UserController
{
    public function collection(\Silex\Application $app)
    {
        return $app['twig']->render('user/collection.html.twig', [
            'users' => $app['user.repository']->collection()
        ]);
    }
}

$app->mount(prefix, ControllerProvider)添加ControllerProvider前缀为prefix的所有路由。例:

class UserControllerProvider implements ControllerProviderInterface
{
public function connect(Application $app)
{
    $controllers = $app['controllers_factory'];

    // url: /user
    $controllers->get("/", '\\App\\User\\UserController::collection')
                ->bind('user.collection');

    // url: /user/111
    $controllers->get("/111", '\\App\\User\\UserController::somemethod')
                ->bind('user.route111');

    return $controllers;
}
}

$app->mount('/user', new UserControllerProvider())