I deployed my php/Yii2 project (based on the basic template) to a shared hosting webspace. I want to access the project via an subdomain because an wordpress is running on the main domain. The normal controllers of the base project are working fine. The problem is that the framework can't find any controller that is in a module. The module itsef is registered properly.
The folder structure in the webspace is the following:
/
www
_wp (target folder of the main domain)
_project
web (target folder of the sub domain)
runtime
views
...
modules
testmodule
Module.php
views
models
controllers
The modules are configured like this:
'modules' => [
'testmodule' => [
'class' => 'app\modules\testmodule\Module',
],
],
The UrlManager is configured like this:
[
'showScriptName' => false,
'enablePrettyUrl' => true,
'enableStrictParsing' => false,
'rules' => array(
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'<module:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>' => '<module>/<controller>/<action>',
'<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>'
),
];
The .htaccess file in the _project\web looks like this:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php? [L]
In the _project folder I do not have an .htaccess file.
On my local machine everything is working fine. On my webspace the controllers of the project itself are working fine. As soon as I navigate in a module i get an 404 Error of the yii framework. I debugged the project and found out that the module itself is loaded correctly. The problem is that the framework can not find the Controller Class because the function class_exists(controller) returns false. I don’t have an idea what i can do to fix this. Hopefully somesone here can help me, thanks a lot!
thanks to the comment of Michal Hynčica! I wrote my controllers lower case and yii2 is looking for them with CamelCase. This worked on windows. But on my shared hosting based on this did not work because of Case-sensivity!
Related
tl;dr My Yii 2 app, running as a micro-framework, works just fine when using standard ("not pretty") URLs. However when the urlManager application component is enabled in the configuration, the entire application breaks completely and every call to it ends with 404.
Following my other question, I am trying to run a minimalist version (micro framework) of Yii 2 app to act as an RESTful endpoint. I've followed the guide and everything seems to be working when I am calling a standard ("not pretty") URLs:
As suggested in the guide, I have enabled routing / pretty URLs in my application:
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'post'],
],
]
And suddenly my application stops working at all. It serves neither non-pretty:
nor pretty URLs:
It even fails to serve the default controller and action -- site/index.
What am I missing?
It seems that I have everything that I need. Following the guide I have:
Created the micro-framework application skeleton
Created default controller (site) and default action (index)
Configured db component, run migrations and created database
Created PostController
Enabled routing by adding Yii code to application configuration (see code example above) and by creating corresponding .httaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
What else must I do in order to make the whole thing work or what am I missing here? Thanks!
Remove this line:
'enableStrictParsing' => true,
This question comes from an misunderstanding. I wasn't aware that when pretty URLs are enabled then yii\rest\UrlRule::$pluralize is set to true by default. Meaning that the request must be:
http://localhost/micro-app/posts
(plural controller name; notice "s" at the end)
Not:
http://localhost/micro-app/post
(singular controller name)
It is also completely normal that when pretty URLs are enabled then regular URLs doesn't work anymore. That's the reason for getting 404 File Not Found on http://localhost/micro-app/index.php?r=post URL when pretty URLs are enabled.
I am a newbie looking forward to set yii2 and experiment with it.
I set a containter in proxmox with yiiframework turnkey.
Then installed adminlte and now I am trying to get backend and frontend work.. but I can't.
I tried with prettyUrls enabled and disabled but simply https://192.168.1.3/frontend/web/index.php or https://192.168.1.3/backend/web/index.php give me back the 404 error "The requested URL /frontend/web/index.php was not found on this server."
Everything is fresh and clean as just barely installed, except for this :
'view' => [
'theme' => [
'pathMap' => [
'#app/views' => '#backend/views'
],
],
],
'assetManager' => [
'bundles' => [
'dmstr\web\AdminLteAsset' => [
'skin' => 'skin-blue',
],
],
],
that is the adminlte integration.. nothing that could even screwed something up.
I tried to set up a .htaccess file in the root with this inside:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
and two different .htaccess files for frontend and backend....
even followed different guides like this one: https://devreadwrite.com/posts/htaccess-for-yii-2-advanced
But It seems that I do not have this frontend and backend separation but just 192.168.1.3 site...
Thanks a lot,
Waiting for some help.
Lordcroci
the frontend and backend are available when using the advanced template. To create a project with the advanced template you should use:
composer create-project --prefer-dist yiisoft/yii2-app-advanced yii-application
being yii-application the name of your project. After that you should initialize the project, using:
/path/to/php-bin/php /path/to/yii-application/init
After that, you should get the web folders and config files for both backend and frontend available, including the index.php
It's only after those steps are completed that you can change the prettyUrl in their respective config/main.php files (frontend and backend).
https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/start-installation.md
you can't access backend and frontend by these url you put in question
https://192.168.1.3/frontend/web/index.php or https://192.168.1.3/backend/web/index.php
use this
https://192.168.1.3/frontend/web/site/index.php or https://192.168.1.3/backend/web/site/index.php
1.first of all after installing yii2 by using this commant
composer create-project --prefer-dist yiisoft/yii2-app-advanced yii-application
2.you must use init command so index file will be available to access the url. After install project just enter to project folder and open terminal and enter command
./init
and select the development option. after this the index file will be available in web folder in both backend and frontend folder.
3.Create a database with any name you want and update common/config/main-local.php file by entering the database detail.
4.Go to your project folder and run command
./yii migrate or yii migrate
it will create two table in your database name users and migrate.
after this add the htaccess file to your backend and frontend's web folder
RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php
and enable pretty url in config file in backend and frontend and uncomment the url manager code.
5.after this you can access the backend and frontend by
https://hostname/backend/web/site/index.php and
https://hostname/frontend/web/site/index.php
but in the question you write this
backend/web/index.php
frontend/web/index.php
which is wrong.
Just access frontend and backend by this
https://hostname/backend/web/site/index.php and https://hostname/frontend/web/site/index.php
and you are good to go.
I am trying to set-up Yii2 to use pretty URL.
In order to do so, I configured Yii2 and used the rewrite module of apache to make sure that we always enter by the entry point which is index.php
I made the following modification in the .htaccess file contained in the yii-application/frontend/web/ folder - folder that contains index.php (advanced yii2 template). For those modifications, I followed instructions found on various forums.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
I have also made the following changes in the configuration of yii2 in order to activate pretty URL
$config = [
'components' => [
'urlManager' => [
'showScriptName' => false,
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
],
];
Note that Yii2 was working well before I made those changes.
Now if I try to connect using one of the following URL (like before modification), I see the landing page. The navigation will not work, I will always see the landing page.
http://localhost/frontend/
http://localhost/frontend/index.php
http://localhost/frontend/index.php?r=site/about-us
http://localhost/frontend/index.php?r=site/faq
If I try to connect using of the URLs below (as I should once pretty URL is configured properly), my brower displays an error message.
http://localhost/frontend/site/faq
http://localhost/frontend/site/account
http://localhost/frontend/site/index
Error message:
Not Found
The requested URL /web/yii-application/frontend/web/index.php was not found on this server.
Apache/2.4.9 (Win32) PHP/5.5.12 Server at localhost Port 80
However, it looks like the path is correct. The index.php file is actually in the folder C:\web\yii-application\frontend\web\index.php
How come my browser does not find the file?
Thanks for your help
From all your requests you are missing the web folder.
All your URLs should be
http://localhost/frontend/web/
http://localhost/frontend/web/index.php
http://localhost/frontend/web/index.php?r=site/about-us
http://localhost/frontend/web/index.php?r=site/faq
http://localhost/frontend/web/site/faq
http://localhost/frontend/web/site/account
http://localhost/frontend/web/site/index
Unless you set up a folder redirection but considering how you have things set up I seriously doubt that.
I turned on pretty URLs in Yii according to the docs
http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html
and the home page is visible, but when I try to go to another URL, it gives me an error.
http://localhost:81/xxx/web/shopping/search?q=toaster
The requested URL /xxx/web/shopping/search was not found on this server.
I tried to create a generic rule, but it didn't help.
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
// ...
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
The docs specifically say not to include the subfolders in the path.
Note: Rules with server names should NOT include the subfolder of the entry script in their patterns. For example, if the application is under http://www.example.com/sandbox/blog, then you should use the pattern http://www.example.com/posts instead of http://www.example.com/sandbox/blog/posts. This will allow your application to be deployed under any directory without the need to change your application code.
Must I list every single route explicitly? I tried to give an explicit route for
'shopping/search' => 'shopping/search',
But it didn't help.
Try this
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => 'false'
],
It was because of Apache. Without pointing to index.php, Apache didn't know where to send the request and is the one giving the error. This will default to index.php.
# For pretty URLs
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
https://komarashettynageshrao.wordpress.com/2010/02/04/pretty-urls-in-yii/
Well, PHP times.
My client wants me to use Yii2 as the framework for his project.
I got it up and running. No problem. I used the advanced template via composer.
Set my web root to /frontend/web, etc.
NOW, i want to use this url format
website.com/messages/ or website.com/messages/tom... etc.
Right now the way is setup shows website.com/index.php?r=messages/index...
I found this documentation...
https://github.com/yiisoft/yii2/blob/master/docs/guide/url.md
But i can't seem to get it straight.
Here are my steps...
I configured my apache server to point to /usr/www/payroll/frontend/web/
I added to my web folder a .htaccess file with this content.
RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php
I also added the component 'urlManager' as in the directions. It seems to catch the request and modify it.
'components'=>[
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => 'false'
],
],
For example if I type website.com you can see it adds /site/index to the url. (without the url component active it simply adds /index.php?site/index)
So, obviously there's a modification perfomed to the url (via UrlManager) but I get 404 error
I am running out of ideas here. I am new to Php, Apache and Yii2. Any help, Greatly appreciated.
Thanks
To make pretty URL working on Yii 2.0 you need 2 things:
1: Edit /frontend/config/main.php (or the appropriate main config in your case) and add:
'components'=>[
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
],
2: Add a .htaccess file in YOUR WEB ROOT folder.
In yii 2.0 advanced this is NOT project root directory but instead:
/frontend/web
.htaccess example:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
just change 'showScriptName' => 'false' to 'showScriptName' => false and it will work
Ok, here is the solution.
In recents version of Apache (from 2.3.9), AllowOverride is set to NONE by default. Previous versions have AllowOverride set to ALL.
Yii2 assumes that AllowOverride will be set to ALL.
If you want to read the whole thread at Yii Forum, here is the link
http://www.yiiframework.com/forum/index.php/topic/53295-url-manager-for-seo-friendly-url-404-error-code/
Thank you for your help and messages!
For Yii2 basic, I just added the codes below to /myappfolder/config/web.php:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName'=>false,
]
Also, added .htaccess in /myappfolder/web/ :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
This works well for me. Hope this helps others who have same problem. : )
If you want to use like this http://domain.com/controller_name/action_name , you only need to enable pretty url in your config file :
'components'=>[
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => 'false'
],
],
Now you can use urls as you wish `website.com/messages/ or website.com/messages/tom.
If you want to use query string in your url this is how it works now in Yii 2 website.com/message?id=12