路由在Silex中定义,但404错误

时间:2015-11-04 14:29:22

标签: php silex

我在项目中使用Silex框架。

我定义了一条道路/登录,指向我的身份验证形式以连接到应用程序,但是当我这样做时:ipsrv / login(ip​​srv是ip web服务器)他不知道道路(404)。

我的app.php文件(摘录):

$app->register(new Silex\Provider\SecurityServiceProvider(), array(
    'security.firewalls' => array(
        'login' => array(
            'pattern' => '^/login',
            'anonymous' => true,
            'form' => array('login_path' => '/login', 'check_path' => '/login_check'),
            'users' => $app->share(function () use ($app) {
                return new Nautilus\DAO\UserDAO($app['db']);
            })
        ),
        'general' => array(
            'pattern' => '^/',
            'anonymous' => false,
            'logout' => true
        ),
    ),
));

我的routes.php文件(摘录):

$app->match('/login', function(Request $request) use ($app) {
    return $app['twig']->render('login.html.twig', array(
        'error'         => $app['security.last_error']($request),
        'last_username' => $app['session']->get('_security.last_username'),
    ));
})->bind('login');

3 个答案:

答案 0 :(得分:0)

你如何使用这两个文件?检查服务器是否配置为执行您的silex应用程序,并且这两个文件实际上都包含在那里。

它在单脚本应用程序中运行良好:

<?php
// web/index.php
require_once __DIR__.'/../vendor/autoload.php';

$app = new Silex\Application();

$app->register(new Silex\Provider\SecurityServiceProvider(), array(
    'security.firewalls' => array(
        'login'   => array(
            'pattern'   => '^/login',
            'anonymous' => true,
            'form'      => array('login_path' => '/login', 'check_path' => '/login_check'),
            'users'     => $app->share(function () use ($app) {
                return new \Symfony\Component\Security\Core\User\InMemoryUserProvider();
            })
        ),
        'general' => array(
            'pattern'   => '^/',
            'anonymous' => false,
            'logout'    => true
        ),
    ),
));

$app->match('/login', function(\Symfony\Component\HttpFoundation\Request $request) use ($app) {
    return 'login form';
})->bind('login');

$app->run();

运行Web服务器时
php -S localhost:8080 -t ./web/

curl http://localhost:8080/login返回&#34;登录表单&#34;;

答案 1 :(得分:0)

文件routes.php完整:

<?php
use Symfony\Component\HttpFoundation\Request;

// Index
$app->match('/', function () use ($app){
    return $app['twig']->render('aff_index.html.twig');
})->bind('home');

// Login form
$app->match('/login', function(Request $request) use ($app) {
    return $app['twig']->render('login.html.twig', array(
        'error'         => $app['security.last_error']($request),
        'last_username' => $app['session']->get('_security.last_username'),
    ));
})->bind('login');

文件app.php完整:

<?php
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\Debug\ExceptionHandler;

// Register global error and exception handlers
ErrorHandler::register();
ExceptionHandler::register();

// Register service providers.
$app->register(new Silex\Provider\DoctrineServiceProvider());

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => array(
        __DIR__.'/../views/layout',
        __DIR__.'/../views/admin_bdd',
        __DIR__.'/../views/admin_preferences',
        __DIR__.'/../views/admin_users',
        __DIR__.'/../views/app_connexion',
        __DIR__.'/../views/app_main',
        __DIR__.'/../views/audits_mesedits',
        __DIR__.'/../views/reporting_lb',
        __DIR__.'/../views/reporting_meteo',
        __DIR__.'/../views/menu'
    )));

$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
$app->register(new Silex\Provider\SessionServiceProvider());
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
    'security.firewalls' => array(
        'login' => array(
            'pattern' => '^/login',
            'anonymous' => true,
            'form' => array('login_path' => '/login', 'check_path' => '/login_check'),
            'users' => $app->share(function () use ($app) {
                return new Nautilus\DAO\UserDAO($app['db']);
            })
        ),
        'general' => array(
            'pattern' => '^/',
            'anonymous' => false,
            'logout' => true
        ),
    ),
));

$app->register(new Silex\Provider\MonologServiceProvider(), array(
    'monolog.logfile' => __DIR__.'/../var/logs/nautilus.log',
    'monolog.name' => 'Nautilus',
    'monolog.level' => $app['monolog.level']
));
$app->register(new Silex\Provider\ServiceControllerServiceProvider());
if (isset($app['debug']) && $app['debug']) {
    $app->register(new Silex\Provider\HttpFragmentServiceProvider());
    $app->register(new Silex\Provider\WebProfilerServiceProvider(), array(
        'profiler.cache_dir' => __DIR__.'/../var/cache/profiler'
    ));
}

$app['dao.user'] = $app->share(function ($app) {
    return new Nautilus\DAO\UserDAO($app['db']);
});

Index.php:

<?php

require_once __DIR__.'/../vendor/autoload.php';

$app = new Silex\Application();

require __DIR__.'/../app/config/dev.php';
require __DIR__.'/../app/app.php';
require __DIR__.'/../app/routes.php';

$app->run();

有关信息,我在Windows 10 Pro x64上使用Wamp(最新版本)和MySQL数据库。我使用的是PhpStorm 10.0。

答案 2 :(得分:0)

问题解决了。 Xampp与WIndows 10不兼容(无法创建用户:错误),Wamp不会处理URL重写Windows 10.我已经使用Windows 7和Xampp进行了URL重写和虚拟主机测试,并且不用担心Silex道路。