When I go to http://www.example.com/new/index.php/login/
(please note /index.php/
as part of the url.)
After successful login, I get redirects to http://www.example.com/new/welcome/
and that’s correct.
but the login screen url should not have /index.php/
as this is Silex restapi.
But when I try login without /index.php/
that would be
http://www.example.com/new/login/
after login this time, I get redirected to new/index.php
instead of /welcome/
like last time.
Please help.
my code is below:
Index.php:
$app = Silex\Application;
$app->mount('/login', new Routers\Login());
$app->run();
Routers\Login.php:
namespace Routers;
use Silex\Application;
use Silex\Api\ControllerProviderInterface;
use Symfony\Component\HttpFoundation\Request ;
class Login implements ControllerProviderInterface
{
public function connect(Application $app)
{
// creates a new controller based on the default route
$controllers = $app['controllers_factory'];
$controllers->get('/', 'Controllers\\Login::index');
$controllers->post('/', 'Controllers\\Login::validate');
return $controllers;
}
}
Controllers\Login.php:
namespace Controllers;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
class Login {
public function index(Request $request, Application $app)
{
return $app['twig']->render('login.html');
}
public function validate(Request $request, Application $app)
{
// validation goes here
if ( // invalid ) {
return $app['twig']->render('login.html');
} else {
// valid
header("Location: /welcome");
exit;
}
}
}
htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>
EDIT:
I guess I discovered the issue, the login form is:
<form method="post" action="index.php">
instead of posting data to http://www.example.com/new/login
So how the action url must be? I tried action=“/new/login” and it doesn’t work.
I get no route for POST /login. but this is defined in Routers/Login.php, so why should I get this?
Please advise.