2
votes

I am new in cakephp trying to loading default controller as Pages

This is my route :

Router::redirect ('/', array('controller' => 'pages', 'action' => 'display'));

Router::connect('/pages/**', array('controller' => 'pages', 'action' => 'display'));

When I run "http://localhost/project/index.php" then its working fine but try with "http://localhost/project/" its not loading default controller (Pages)

Without htaccess & with htaccess its giving same issue.

This is error:

Controller class ProjectController could not be found.

Error:

The requested address '/project/index.php/project/' was not found on this server.

3
Did you changed .htaccess filePradeep Singh
Yes I tested with htaccess and without htaccess but didn't run default on root "/"Surendra Suthar
this is my htaccess (webroot): <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] </IfModule>Surendra Suthar
Its not the same that which come with cakephpPradeep Singh
.htaccess isn't issue I have added default htaccess but didn't run.Surendra Suthar

3 Answers

0
votes

You are running in subdirectory, so you should set RewriteBase:

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase /project/
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule ^(.*)$ /project/index.php?url=$1 [QSA,L] 
</IfModule
0
votes

As per cakephp manual :

http://book.cakephp.org/3.0/en/development/routing.html#redirect-routing

You should try

Router::scope('/', function ($routes) {
$routes->redirect(
    '/home/*',
    ['controller' => 'Articles', 'action' => 'view'],
    ['persist' => true]
    // Or ['persist'=>['id']] for default routing where the
    // view action expects $id as an argument.
);
})

Instead of Router::redirect.You should try this method once, might issue got resolved.

0
votes

Its resolved

Added baseUrl in app controller:

function beforeRender(){
  $this->set('baseUrl', 'http://'.$_SERVER['SERVER_NAME'].Router::url('/'));
}

Removed App.baseUrl from Core.php :

Configure::write('App.baseUrl', env('SCRIPT_NAME'));

now its working fine on: http://localhost/app/