Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route only takes absolute path #17

Closed
blebon-km opened this issue May 20, 2016 · 6 comments
Closed

Route only takes absolute path #17

blebon-km opened this issue May 20, 2016 · 6 comments
Assignees
Labels
Milestone

Comments

@blebon-km
Copy link

If your wordpress is not located at the root of your webserver you must specify the absolute path in each routes which is, in my opinion, counterintuitive. The application should behave the same way whether it's located at the root or in a subfolder of the webserver !
Actually it took me time to understand why my routing wasn't working ! If you don't plan to fix it immediatly I think it could be nice to have a few words about it in the documentation ;)

@victorximenis
Copy link

victorximenis commented May 20, 2016

Hi, @blebon-km.
If i understand correctly, you have two problems, right?
For the second, Alter uses WordPress Rewrite API.
The Rewrite API needs .htaccess to work fine.
Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

If you are using docker, i think this is provided to you by default, but if you are not, try this solution.

For your first problem, can you give me more details about your environment?

@blebon-km
Copy link
Author

Hello,

I think you misunderstood my "problem", so I'll explain with an example:
Let's consider that I have a wordpress located at the URL http://localhost/my/subfolder/wordpress in my developpement environment.
Now I'm adding a new Route:

Route::get('/hello', function(){
  echo "Hello !"
});

Then, when I type the URL http://localhost/my/subfolder/wordpress/hello I expect to see the message "Hello" but instead I get a 404 error.

To make it work, I need to change my code to replace the relative path with an absolute path like this:

Route::get('/my/subfolder/wordpress/hello', function(){
  echo "Hello !"
});

This can be annoying because in a production environnent the URL of my wordpress won't be the same so I'll have to change all my routes to make it work.

@sergiopvilar
Copy link
Member

sergiopvilar commented May 20, 2016

@blebon-km Thanks for reporting! This is a know problem actually, I'll give you a heads up when I do the fix. For now I advice you to create a subdomain to your subfolder installation, if it's possible. Sorry about that!

@sergiopvilar sergiopvilar added this to the 0.2.1 milestone May 20, 2016
@sergiopvilar sergiopvilar self-assigned this May 20, 2016
@sergiopvilar
Copy link
Member

@blebon-km Alter is built on top of FastRoute, please take a look on this issue and tell me if any of the solutions works.

@blebon-km
Copy link
Author

Actually I took a look in the Ampersand project (the Route.php file) and I think the problem might be either located in the addRoute method around the line 134:

    $_route = $this->getPrefix($parameters[0]);
    $broken = array_values(array_filter(explode('/', $_route)));
    $robj = [];
    $robj['methods'] = $methods;
    $robj['callback'] = $parameters[count($parameters) -1];

    foreach($broken as $part){
      if(preg_match('/^:[a-zA-Z0-9]+/', $part))
        $_route = str_replace($part, "{".substr($part, 1)."}", $_route);
    }

    $robj['route'] = $_route;

The $_route variable contains a relative path so maybe we should turn it into an absolute path ?

or either in the parse_request method around the line 265:

    $httpMethod = $_SERVER['REQUEST_METHOD'];
    $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

    if(strpos($uri, '/wp-admin') > -1) return;

    if($uri == '/404' || $uri == '/404/')
      http_response_code(404);

    $routeInfo = $dispatcher->dispatch($httpMethod, $uri);

At this moment the $uri variable contains the absolute path, so it won't match any route that has a relative path.

But I'm not sure if these are the right places to fix this "problem". What's your opinion about it ?

Anyway, there's also a workaround that consists in nesting all the routes in a Route::group that has the base URI of the application as a path which simplify a lot the transition between the development and the production environment.

@sergiopvilar
Copy link
Member

@blebon-km sorry for the long delay! This should be fixed in Ampersand's latest release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants