Slim: Slim 3 cannot work in subdirectory. Cannot work when accessed from localhost/slim_app_dir
Hey Folks 😃
Slim 3 cannot work in subdirectory. Cannot work when accessed from localhost/slim_app_dir.
BUT, no problem at all when using virtual host. I really really need to run slim 3 on sub directory.
I create / main route. It;s ok when accessed via virtual host.
$app->get('/', function ($request, $response, $args) {
if (isset($_SESSION['Auth'])) {
return $response->withStatus(302)->withHeader('Location', '/dashboard');
}
return $this->view->render(
$response,
'login/index'
);
});
BUT, the route / never matched when accessed from localhost/slim_app_dir
Because the main url route, from / become /slim_app_dir
And I don’t want to create url route named /slim_app_dir , because, i will crazy because i must create url route /slim_app_dir_renamed when i rename the directory.
Please help me Thank you
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 2
- Comments: 15 (4 by maintainers)
I have a very simple fix for you to get Slim 3 running in a subdirectory:
I will show you how to fix the
slim/slim-skeletonapp:Create a new project (or take your existing project)
cd c:\xampp\htdocs\composer create-project slim/slim-skeleton slim3-appCreate a new file:
c:\xampp\htdocs\slim3-app\.htaccessContent of .htaccess
Open:
c:\xampp\htdocs\slim3-app\src\dependencies.phpAdd this lines to fix it:
Now you can open the app via:
http://localhost/slim3-app.aahhh thank you very much @r3wt and @silentworks , my problem solved! it’s because Garret advises me to not over complicate things… hahahaha thx by the way 😃
Now my ap structure looks like below :
The source of the problem is putting index.php and .htaccess in
slim3-app/publicaahh bad…bad… soo… i remove index.php and .htaccess inslim3-app/publicand then use only one index.php and .htaccess inslim3-app/my .htaccess
i’m so sorryy… I’ve been prejudiced that this is Slim 3 router bugs.
Thank you very much guys 😃
Why put it in a sub directory at all? just alias it. i have 3 slim apps running in the root directory along with an angularjs front end that just loads from an html index file, all sharing a backend codebase off in another directory. you should be able to do this same thing in apache i’m sure.
example:
so in each of
api.php,admin.php, anddebug.phpi have a group wrapping all the routes like soWhy over complicate things by using an actual directory? i don’t see the advantage of that at all.
@okaprinarjaya, it works for me Thanks!
@okaprinarjaya, Yep, it works for me and I fix it like you said. Thanks!
Make use of Route names and pathFor, otherwise you will have to work out the context of subdirectories by yourself.