acelaya / expressive-slim-router

A router for Zend Expressive based on Slim framework's implementation
MIT License
7 stars 2 forks source link

I can't use the slim router in my expressive application #4

Closed shishi666 closed 5 years ago

shishi666 commented 5 years ago

Hy,

I created an expressive application with the following choices: PlatesRenderer, FastRouter, ServiceManager

I added this line in my composer.json file

require "acelaya/expressive-slim-router" : "^4.0.0"

I added this line in my routes.php file

$app->get('(/:lang)/', [
        HomeHandler::class
], 'home');

$app->route('(/:lang)/contact-me/', [
        ContactHandler::class,
], ['GET'], 'contact-me');

Only when I try the following url: 'https://www.mywebsite.com/' and '' https://www.mywebsite.com/contact-me/'

I always get my homepage.

I do not understand what I forgot to do or where the error came from. Would you have a search track please?

thank you in advance

Shishi

acelaya commented 5 years ago

You say you installed fast route. Did you remember to disable the ConfigProvider from that component and enable the one from this one?

Even more. Did you uninstall the fast route router?

Have you tried changing the route definition order? And completely removing the home route? Does the contact route load at least in that case?

There's also a typo in your config, but I don't think it has anything to do with this. The first route is not defining the list of allowed methods as third param, but the route name.

shishi666 commented 5 years ago

thank you for your reply

You say you installed fast route. Did you remember to disable the ConfigProvider from that component and enable the one from this one?

I added this line in my config.php file: ` \ Acelaya \ Expressive \ ConfigProvider :: class ` and I disabled the ConfigProvider for the FastRouter component

Even more. Did you uninstall the fast route router?

yes

Have you tried changing the route definition order? And completely removing the home route? Does the contact route load at least in that case?

if I change the order of the roads or if I delete the road home I get the template of my road contact-me

There's also a typo in your config, but I don't think it has anything to do with this. The first route is not defining the list of allowed methods as third param, but the route name.

I call the function get for the home route

public function get (string $ path, $ middleware, string $ name = null): Router \ Route
{
    return $ this-> route ($ path, $ middleware, ['GET'], $ name);
}

Thank you again for your help

acelaya commented 5 years ago

If by changing the order of the routes it works, I would go with that approach. It probably has something to do on how the slim router works.

I will make some tests though. Which expressive version are you installing? How are you serving the app (Apache, nginx, built-in server, swoole...)?

shishi666 commented 5 years ago

I'm using version 3.0.1 of zend-expressive, I installed apache 2.4 and php 7.2 and I don't use swoole.

in the meantime I will use the approach you quoted above.

Thank you in advance for your investigations

acelaya commented 5 years ago

I just need one more thing in order to reproduce the issue. How is your apache mod_rewrite configuration?

shishi666 commented 5 years ago

In my httpd.conf file I just uncommented the line

LoadModule rewrite_module modules / mod_rewrite.so

In my httpd-vhost.conf file:

<VirtualHost *: 80>
        ServerAlias ​​shishi.fr
        DocumentRoot "E: \ dev \ php \ shishi.fr \ public"
        <Directory "E: \ dev \ php \ shishi.fr">
                Options Indexes FollowSymLinks
                AllowOverride All
                Require all granted
        </ Directory>
</ VirtualHost>

And to finish in the public directory of my project I left the file .htaccess by default to know:

RewriteEngine On
# The following policy allows authentication to work with fast-cgi
RewriteRule. * - [E = HTTP_AUTHORIZATION:% {HTTP: Authorization}]
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond% {REQUEST_FILENAME} -s [OR]
RewriteCond% {REQUEST_FILENAME} -l [OR]
RewriteCond% {REQUEST_FILENAME} -d
RewriteRule ^. * $ - [NC, L]

# The following rewrites all other queries to index.php. Tea
# condition ensures that you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size
# fits all solution.
RewriteCond% {REQUEST_URI} :: $ 1 ^ (/.+) (. +) :: \ 2 $
RewriteRule ^ (. *) - [E = BASE:% 1]
RewriteRule ^ (. *) $% {ENV: BASE} index.php [CN, L]

Do not hesitate if you need more information. Thank you for investing time on my problem

acelaya commented 5 years ago

The apache config looks good, but to be honest, I have not used this library (or plain slim router v2) with anything other than nginx, so I'm not sure what could be the difference.

I use expressive-slim-router in my website, with a configuration similar to yours, and it has always worked.

My guess is that the issue is comming from slim router itself (I remember it was not super reliable), but version 2 is unmaintained, so I don't think they accept new issues.

Maybe you could try to do a quick proof of concept creating a small slim 2 project with those 2 routes and the same apache config, and see if it has the same issue.

shishi666 commented 5 years ago

Thank you for considering my problem. I have advanced a bit in my search and adding my routes in the routes.php file like this:

$app-> get('(/:lang)/', [
        HomeHandler::class
], 'home');

does not add any conditions to the 'lang' parameter, however, defining the route like this:

$route = new Zend\Expressive\Router\Route('(/:lang)/', HomeHandler, 'GET', 'home');
$route-> setOptions([
     'conditions' => [
        'lang' => 'en|fr',
     ],
]);
$app->route($route);

it works another solution is to inject the routes via the configuration using the delgators provided by zend-expressive (as you do for your website): in ConfigProvider

public function getDependencies(): array
{
    return [
        'factories' => [
                Handler\HomeHandler::class => ConfigAbstractFactory::class,
        'delegates' => [
            Application::class => [
                ApplicationConfigInjectionDelegator::class,
            ]
        ]
    ];
}

public function getRoutes(): array
    {
        return [
            [
                'name' => 'home',
                'path' => '(/:lang)/',
                'allowed_methods' => ['GET'],
                'middleware' => [
                    FlashMessageMiddleware::class,
                    HomeHandler::class,
                ]
                'options' => [
                    'conditions' => [
                        'lang' => 'en|fr',
                    ]
                ]
            ]
        ];
    }

I close this issue Thank you again for your help