1

I have changed my $defaultRoute on my config which looks like this. This is according to my default controller AdminController? I am getting 404 Error

main-local.php

<?php
$config = [
'defaultRoute' => 'admin/index',
'components' => [
    'request' => [
        // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
        'cookieValidationKey' => 'b0pQ7nOuVjCprrOGrTarC-ErVMHUWQbb',
    ],
  ],
];
if (!YII_ENV_TEST) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module';

$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = 'yii\gii\Module';
}

return $config;

I am testing my app on localhost but I still have my main config with the default root.

main.php

<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-backend',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'bootstrap' => ['log'],
'defaultRoute' => 'admin/index',
'modules' => [],
'components' => [
    'user' => [
        'identityClass' => 'backend\models\Admin',
        'enableAutoLogin' => true,
    ],
    'log' => [
        'traceLevel' => YII_DEBUG ? 3 : 0,
        'targets' => [
            [
                'class' => 'yii\log\FileTarget',
                'levels' => ['error', 'warning'],
            ],
        ],
    ],
    'errorHandler' => [
        'errorAction' => 'admin/error',
    ],
],
'params' => $params,
];

My .htaccess has the following code:

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

# if request begins with /admin remove admin and ad /backend/web/
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^admin\/?(.*) /backend/web/$1

# other requests add /frontend/web/$1
RewriteCond %{REQUEST_URI} !^/(frontend/web|backend/web|admin)
RewriteRule (.*) /frontend/web/$1

# if frontend request 
RewriteCond %{REQUEST_URI} ^/frontend/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /frontend/web/index.php 

# if backend request
RewriteCond %{REQUEST_URI} ^/backend/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /backend/web/index.php

Also here is my AdminController code:

<?php
namespace backend\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use backend\models\LoginForm;
use yii\filters\VerbFilter;
 class AdminController extends Controller
  {
   public function behaviors()
  {
    return [
        'access' => [
            'class' => AccessControl::className(),
            'rules' => [
                [
                    'actions' => ['login', 'error'],
                    'allow' => true,
                ],
                [
                    'actions' => ['logout', 'index'],
                    'allow' => true,
                    'roles' => ['@'],
                ],
            ],
        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'logout' => ['post'],
            ],
        ],
    ];
}

/**
 * @inheritdoc
 */
public function actions()
{
    return [
        'error' => [
            'class' => 'yii\web\ErrorAction',
        ],
    ];
}

public function actionIndex()
{
    return $this->render('index');
}

public function actionLogin()
{
    if (!\Yii::$app->user->isGuest) {
        return $this->goHome();
    }

    $model = new LoginForm();
    if ($model->load(Yii::$app->request->post()) && $model->login()) {
        return $this->goBack();
    } else {
        return $this->render('login', [
            'model' => $model,
        ]);
    }
}

public function actionLogout()
{
    Yii::$app->user->logout();

    return $this->goHome();
}
}

Am I inserting the 'defaultRoute' => 'myController/myAction', in the wrong file. I do not want to do it on the framework code itself so I would really appreciate your assistance.

Zack
  • 1,527
  • 2
  • 20
  • 32

2 Answers2

2

Your placement is correct for the 'defaultRoute' property.

Have you tried setting it to something other than admin/index that doesn't have a rule in your .htaccess?

If this it works then you know your .htaccess is conflicting with it and I'd try the following.

I'd remove the custom .htaccess rules (i'll refer to them as blocks 1,2,4) one at a time leaving the other blocks. If that doesn't work then remove 1 & 2 together; then I'd try all. By doing this it should help you narrow down the problem.

Note that if you do remove the rules then you will have to adjust your url accordingly for the tests (i.e. add backend/web/ back to your url so yourAppName/backend/web should get you to your admin/index) as you won't have the rules removing backend/web from your URL for you.

If it's not the .htaccess then it would have to be some other code that you have in your app that is custom.

Here is an .htaccess rule to remove .index

#Use if on shared hosting and this not in the root folder.
#RewriteBase /

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
skworden
  • 21
  • 3
  • I am currently trying to find the problem using your suggestion of setting something else in the `defaultRoute.` So this have helped me move a step ahead. I have introduced a test Controller `Settings` and it actually works. The problem is when I change back to my admin controller it redirects to `index.php?r=site/login.` I havent found out why yet though. – Zack Jul 09 '15 at 06:55
  • I would assume it's your access rules that are making it redirect. If this is the case then only guest are redirected and not logged in users. You would have to add 'index' to your first access rule in your controller's "access" property, however, that would allow not logged in users to view an admin page. I also updated the answer to remove r=index.php from your url however, you will also need to update your urlmanager to reflect the changes. It will have to be exactly like @Bharat Chauhan posted. The rules can of course be different. – skworden Jul 09 '15 at 14:13
  • Have you looked at my current .htaccess? This looks exactly the same except for the fact that yours is for basic template. – Zack Jul 09 '15 at 18:06
  • Yes, it could be used on the basic template however, that is the first part of my advanced templates .htaccess and it works. Do you have a .htaccess in your frontend/web and in your backend/web? It appears you dont and need to have them in both places. Post your folder structure because your htaccess is wrong and without it nobody can't help you correct it. – skworden Jul 09 '15 at 21:23
-1

Take it out of the main-local.php

Inside main.php it is in the correct location thou.

'defaultRoute' => 'admin/index',

Note that you need to place AdminController in the @backend/controllers directory as specified in this statement in your code.

'controllerNamespace' => 'backend\controllers',
n099y
  • 414
  • 2
  • 16
  • Still getting error 404. I already had my AdminController in backend/controllers directory. – Zack Jul 07 '15 at 12:55
  • I would paste your .htaccess, because with no urlmanger rules and that setup it should be routing them all to your default controller. – n099y Jul 07 '15 at 13:02