404 using defaultRoute in apps main config

Hi everyone

I’ve seen alot of discussions about 404 errors here. So here is another one! :rolleyes:

I guess there is a very simple answer to my question but I really need to get moving forward now in my main project.

I have trouble in my main project(advanced template) with 404 on my Debian LAMP installation, it looks like yii can’t render files other than those under views/site folder, otherwise I get a 404. It will not use another controller besides SiteController. I’ve checked all files group and user ownership. They have the same ownership and permissions as views/site folder(works as a sharm under windows though(Wamp setup). So I’ve been crying blood because of this. So I decided to try to reproduce my problem. I created a new advanced template in /var/www and renamed advanced folder to test.

Here’s my network setup with apache2.

in my hosts file it looks like this

/etc/hosts:

127.0.0.1 localhost

127.0.0.1 test

The following lines are desirable for IPv6 capable hosts

::1 localhost ip6-localhost ip6-loopback

ff02::1 ip6-allnodes

ff02::2 ip6-allrouters

in my apache2 conf file:

ServerAdmin webmaster@localhost

ServerName test

ServerAlias test

DirectoryIndex index.php

DocumentRoot /var/www/test/frontend/web

I’ve reloaded all confs using service apache2 reload and restarted apache2.

Now the standard setup is done and standard pages are up and running.

In my frontend/config/main.php you will see this default option

‘controllerNamespace’ => ‘frontend\controllers’,

under controllerNamespace I have added

‘defaultRoute’=>‘objects/index’,

except defaultRoute everything is standard setup by the template.

I’ve used Gii to create a model called ObjectTypes in frontend/models/ObjectTypes.php

I’ve also used Gii to create views, a controller and Search model.

Below you’ll see a typical default setup with Gii

Views Object:

frontend/views/objects/create.php

frontend/views/objects/_form.php

frontend/views/objects/index.php

frontend/views/objects/_search.php

frontend/views/objects/update.php

frontend/views/objects/view.php

Controller:

frontend/controllers/ObjectTypesController.php

Models:

frontend/models/ObjectTypes.php

frontend/models/ObjectTypesSearch.php

So if I want my application to use ObjectTypesController as default, not SiteController. Am I using wrong setup in my main config file? There is another public property in Applications class which is called controller, but this one is read-only and so defaultRoute is the property to use according to Yii Framework 2 guide. Could it be a problem with permissions somewhere else in the advanced template? Or should I add other properties to my config file?

thanks in advance

// Robert

BTW I’ve also tried with chmod 777 on all files and folders recursively and also added group www-data to all files and folders recursivly

Hi!

Without reading your whole post.

I would say you use defaultRoute or the render function wrong.

Set a default route in config should be:




$config = [

    'id' => 'yyourAppID', 

    'basePath' => dirname(__DIR__),

    'name' => 'TESTSITE',

    ... ... ... ...

    // Default Route where the application starts 

    // its controller/action or module/controller/action

    'defaultRoute' => 'something/create', // leads to fronend/controllers/SomethingController.php => actionCreate



Regarding the rendering of views.

Yii2 looks per default always in "views" folder.

So lets say you have following folders:

frontend/views/something/index.php

frontend/views/objects/someview.php

frontend/views/users/create.php

You can now render different views like this:

frontend/controllers/SomethingController.php inside the action you want.





// render view from SomethingController 

$this->render('index'); 

// render view from ObjectsController

$this->render('/objects/someview'); 

// render view from UserController

$this->render('/users/create');


// render view from different application for example backend 

$this->render('@backend/views/objects/create');



Hope this helps.

Regards

thanks it helped!

I actually noticed that my controllers are named in a false pattern.

False: MyFooController.php

True: MyfooController.php

This works fine on Windows but as you move your files to a Linux server you will have to change these filenames.