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

Less information #71

Closed
MESWEB opened this issue Aug 31, 2015 · 13 comments
Closed

Less information #71

MESWEB opened this issue Aug 31, 2015 · 13 comments

Comments

@MESWEB
Copy link

MESWEB commented Aug 31, 2015

This script has less information about using it. Where is info about .htaccess? Why in basic usage is:
"$r->addRoute('GET', '/user/{id:\d+}/{name}', 'handler2');" this is not working when anyone using subfolder as root of script. because router don't expecting subfolder/user/12/mario.

@olvlvl
Copy link

olvlvl commented Nov 10, 2015

The .htaccess for this kind of thing is pretty standard. This should do the trick:

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

That said, I don't understand what you mean by subfolder. The routes are relative to your DOCUMENT_ROOT.

@AmdY
Copy link

AmdY commented Nov 10, 2015

Set RewriteBase to the subfolder

RewriteEngine On
RewriteBase /subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [QSA,L]

@MESWEB
Copy link
Author

MESWEB commented Nov 10, 2015

@olvlvl :
I have project in subfolder like: localhot/project/cms and folder cms is a root folder for this script. So if You use this code:

$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
    $r->addRoute('GET', '/users', 'get_all_users_handler');
    // {id} must be a number (\d+)
    $r->addRoute('GET', '/user/{id:\d+}', 'get_user_handler');
    // The /{title} suffix is optional
    $r->addRoute('GET', '/articles/{id:\d+}[/{title}]', 'get_article_handler');
});

This code not working because system router expect localhost/users/name/id and we have localhost/project/cms/users/name/id

@olvlvl
Copy link

olvlvl commented Nov 10, 2015

If localhost/project/cms is indeed your root folder, the router should work. When you display $_SERVER['DOCUMENT_ROOT'] do you have localhost/project/cms or localhost? If you have localhost then something is wrong in your config.

@MESWEB
Copy link
Author

MESWEB commented Nov 11, 2015

My $_SERVER['DOCUMENT_ROOT'] = C:/xampp/htdocs this is default settings server after installation completed.

@samwilson
Copy link

@MESWEB Do you need to give the dispatcher the path info? e.g.:

$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
$response = $dispatcher->dispatch($request->getMethod(), $request->getPathInfo());

@snopboy
Copy link

snopboy commented Jan 30, 2016

This issue is still open, if it has been resolved please close it.
A possible solution will be to create a variable that holds the virtual path from the engine root to your application root, something as easy as this:

$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
    $base_url = '/project/cms';
    $r->addRoute('GET', $base_url.'/users', 'get_all_users_handler');
    // {id} must be a number (\d+)
    $r->addRoute('GET', $base_url.'/user/{id:\d+}', 'get_user_handler');
    // The /{title} suffix is optional
    $r->addRoute('GET', $base_url.'/articles/{id:\d+}[/{title}]', 'get_article_handler');
});

Then moving from your local machine to production server, can be as simple as changing $base_url to '' or whatever other path there is.

@MESWEB
Copy link
Author

MESWEB commented Feb 1, 2016

@snopboy Yes I'm using this but this is very bad idea for using better if author make automatic structure of folders.

@Trainmaster
Copy link
Contributor

@MESWEB Why aren't you using path info as @samwilson suggested?

All the necessary information should be in the $_SERVER array. @nikic has already pointed to $_SERVER['REQUEST_URI'] in the basic usage example. Furthermore you should have a look at $_SERVER['SCRIPT_NAME']. That's how I managed to properly initialize path info in different environments. You can have a look at my code here: https://github.com/Trainmaster/Vision/blob/master/src/Vision/Http/Request.php#L280

@MESWEB
Copy link
Author

MESWEB commented Apr 4, 2016

@Trainmaster I'm using this solution but when somebody have different structure. This fix should be a automatic recognize folder structure.

@bwoebi
Copy link
Contributor

bwoebi commented Apr 4, 2016

You also can remove a certain prefix from the URL to be matched against (i.e. leave routes as is, just alter input route). This should be the cleanest way to solve that issue.

Fixing this is not really a concern of FastRoute - it's just matching routes based on input.
Give it correct input and you'll get expected output.

E.g. $dispatcher->dispatch($method, substr($path, strlen("/prefix"));
You may automatically figure out the prefix from docroot and file path maybe...?

@kelunik
Copy link

kelunik commented May 2, 2016

Can we close this issue now?

@nikic
Copy link
Owner

nikic commented Jan 19, 2017

Closing this issue in favor of #110, which talks about the prefix issue more specifically. My conclusion there is: We might add a helper for uri preprocessing to handle the query string / decoding / prefix removal, but this library will not implement any automagic for guessing which URI to match against, as the logic for this across different servers and sapis is involved and there are other libraries dealing with the intricacies already.

@nikic nikic closed this as completed Jan 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants