Gus de Boer
Gus de Boer

Reputation: 401

CakePHP admin and website router

I'm learning CakePHP and I'm building a CMS, but I cant seem to get my router right.

Every url should use the index action of the WebsiteController, except "admin"

/admin works, but when I go to /foobar it asks for FoobarController

Router::connect("/admin/:controller/:action", 
    array("controller" => "admin")
);
Router::connect("/", 
    array("controller" => "website", "action" => "index")
);
Router::connect("/:slug/*", 
    array("controller" => "website", "action" => "index"), 
    array("pass" => array("slug"), "slug" => '(?!admin)')
);

Upvotes: 0

Views: 124

Answers (2)

Gus de Boer
Gus de Boer

Reputation: 401

So, I figured it out.

By removing:

Router::connect("/admin/:action",
    array("controller" => "admin")
);
Router::connect("/",
    array("controller" => "website", "action" => "index")
);

And adding:

Router::connect("/admin",
    array("controller" => "admin", "action" => "index")
);

Router::connect("/:slug",
    array("controller" => "website", "action" => "index"),
    array("pass" => array("slug"))
);

Special thanks to PGBI

Upvotes: 0

PGBI
PGBI

Reputation: 710

I think the problem is the "slug" => '(?!admin)'. You don't need it anyway because an url starting with /admin will already be caught by your first route.

Other problem, the following route does not make sense:

Router::connect("/admin/:controller/:action", 
   array("controller" => "admin")
);`

It basically tells to handle /admin/foobar url by admin Controller and also foobar Controller.

Try this:

Router::connect("/admin",
    array("controller" => "admin", "action" => "index")
);
Router::connect("/admin/:action",
    array("controller" => "admin")
);
Router::connect("/",
    array("controller" => "website", "action" => "index")
);
Router::connect("/:slug/*",
    array("controller" => "website", "action" => "index"),
    array("pass" => array("slug"))
);

Upvotes: 1

Related Questions