# Define CakePHP Route to call specific controller if given action not exists

**URL:** <https://discourse.cakephp.org/t/define-cakephp-route-to-call-specific-controller-if-given-action-not-exists/2608>\
**Category:** Need Help\
**Created:** [May 23, 2017, 5:29am UTC](https://discourse.cakephp.org/t/define-cakephp-route-to-call-specific-controller-if-given-action-not-exists/2608 "2017-05-23T05:29:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![aman-rawat](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/aman-rawat/32/160_2.png) [@aman-rawat](https://discourse.cakephp.org/u/aman-rawat)\
**Post date:** [May 23, 2017, 5:29am UTC](https://discourse.cakephp.org/t/define-cakephp-route-to-call-specific-controller-if-given-action-not-exists/2608/1 "2017-05-23T05:29:11Z")

</div>

> Using CakePHP v3.3.16

I want to write a fallback route in such a way that if URL is not connected to any action then it should go to that fallback.

Created routes for SEO friendly URL like this

```
   $routes->connect(
        ':slug',
        ['prefix'=>'website','controller' => 'Brands', 'action' => 'index'],
        ['routeClass' => 'DashedRoute']
    );
    $routes->connect(
        ':slug/*',
        ['prefix'=>'website','controller' => 'Products', 'action' => 'index'],
        ['routeClass' => 'DashedRoute']
    );

```

But it’s also inclute all the controller actions in it so if i try to call a controller ex: cart/index it’s going to website/brands/index/index

If I have to remove exclude it, I have to create a route like this

`$routes->connect('/cart',['controller' => 'Cart'], ['routeClass' => 'DashedRoute']);`  
And so on to the other controller to access.

**Example:** I have a controller CartController action addCart

> CASE 1

if I access URL my\_project/cart/addCart/ It should go to cart controller action

> CASE 2

if I access URL my\_project/abc/xyz/ and there is no controller named abc so it should go to BrandsController action index

My Current **routes.php** looks like this

```
Router::defaultRouteClass(DashedRoute::class);

Router::scope('/', function (RouteBuilder $routes) {

    $routes->connect('/', ['prefix'=>'website','controller' => 'Home', 'action' => 'index']);
    $routes->connect('/trending-brands', ['prefix'=>'website','controller' => 'Brands', 'action' => 'trending']);
    $routes->connect('/users/:action/*',['prefix'=>'website','controller' => 'Users'], ['routeClass' => 'DashedRoute']);

    $routes->connect('/cart',['prefix'=>'website','controller' => 'Cart'], ['routeClass' => 'DashedRoute']);
    $routes->connect('/cart/:action/*',['prefix'=>'website','controller' => 'Cart'], ['routeClass' => 'DashedRoute']);

    $routes->connect(
        ':slug',
        ['prefix'=>'website','controller' => 'Brands', 'action' => 'index'],
        ['routeClass' => 'DashedRoute']
    );
    $routes->connect(
        ':slug/*',
        ['prefix'=>'website','controller' => 'Products', 'action' => 'index'],
        ['routeClass' => 'DashedRoute']
    );
    $routes->connect(':controller', ['prefix'=>'website'], ['routeClass' => 'DashedRoute']);
    $routes->connect(':controller/:action/*', ['prefix'=>'website'], ['routeClass' => 'DashedRoute']);

    $routes->fallbacks(DashedRoute::class);
});

Router::prefix('website', function (RouteBuilder $routes) {
    $routes->fallbacks(DashedRoute::class);
});

Plugin::routes();

```
