klein / klein.php

A fast & flexible router
MIT License
2.66k stars 290 forks source link

Routing Issue when using klein through custom MVC Framework #305

Open codenathan opened 8 years ago

codenathan commented 8 years ago

I am having issue with the routing in an MVC env, please have a look at the following repo : - https://github.com/codenathan/klein-error

/public/ is the folder every user will go through

I am trying to get the routing to go through my route.php class under /app/core/route.php

all my routes are definited under /app/routes.php

however non of this are working expect -

$klein->respond(function () {
    return 'All the things';
});

I believe this is a misconfiguration in my .htaccess files any help would be greatly appreciated

Please let me know where and which changes i need to make to get this working :)

codenathan commented 8 years ago

@Rican7 Please help?

nbish11 commented 8 years ago

Have you included the route.php file and instantiated the class?

codenathan commented 8 years ago

Hello @nbish11 yes I have ..

  1. If you look through the repo , All users are redirected to the public folder (https://github.com/codenathan/klein-error/tree/master/public).
  2. where within /public/index.php, I am doing an includes of my init.php file and instantiated my App class.
  3. The App class instantiates a new Route class in its construct (https://github.com/codenathan/klein-error/blob/master/app/core/App.php)
  4. Within my route class (https://github.com/codenathan/klein-error/blob/master/app/core/Route.php) i am instantiating a new Klein class and also including my routes file there within the constuct.
  5. I know all of the above is working because the "All of the things" response is set within the routes file (https://github.com/codenathan/klein-error/blob/master/app/routes.php)
  6. Hence why from the above i believe the problem is with the .htaccess
nbish11 commented 8 years ago

Alright then. I'll clone your repository, take a look and get back to you.

codenathan commented 8 years ago

@nbish11 Thank you!

nbish11 commented 8 years ago

The .htaccess is definitely the problem. This is what fixed it for me:

RewriteEngine On

# I've removed "/HubCRM" as I have the "klein-error" project folder inside a virtual host
RewriteBase /public/

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

# Changed the rewrite rule to a more simple one
RewriteRule ^ index.php [L]

Also in your routes.php don't forget to include the /public/ prefix in your URI's or you could setup Klein to work without the prefix Sub-Directory Installation. Example:

// Notice the "/public" prefix in the URI.
$klein->respond('GET', '/public/hello-world', function () {
    return 'Hello World!';
});
$klein->respond('GET','/public/hello',function(){
    return "Hello";
});
codenathan commented 8 years ago

Thank you @nbish11 . Htacess was what I was least confident in and knew it was somewhere there I made the mistake! Thank you again

nbish11 commented 8 years ago

:smiley: Glad to be off assistance.