want to remove action name from url CakePHP

前端 未结 4 1234
一整个雨季
一整个雨季 2020-12-22 07:02

i am working on a Cakephp 2.x.. i want to remove the action or controller name from url ... for example i am facing a problem is like that

i have a function name <

相关标签:
4条回答
  • 2020-12-22 07:46

    REST Routing

    The example in the question looks similar to REST routing, a built in feature which would map:

    GET    /recipes/123    RecipesController::view(123)
    

    To enable rest routing just use Router::mapResources('controllername');

    Individual route

    If you want only to write a route for the one case in the question it's necessary to use a star route:

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

    Usage:

    echo Router::url(array(
        'controller' => 'messages',
        'action' => 'messages',
        823214
    ));
    // /messages/823214
    

    This has drawbacks because it's not possible with this kind of route to validate what comes after /messages/. To avoid that requires using route parameters.

    Router::connect('/messages/:id',
        array(
            'controller' => 'messages',
            'action' => 'messages'
        ),
        array(
            'id' => '\d+',
        )
    );
    

    Usage:

    echo Router::url(array(
        'controller' => 'messages',
        'action' => 'messages',
        'id' => 823214 // <- different usage
    ));
    // /messages/823214
    
    0 讨论(0)
  • 2020-12-22 07:54

    When connecting routes using Route elements you may want to have routed elements be passed arguments instead. By using the 3rd argument of Router::connect() you can define which route elements should also be made available as passed arguments:

    // SomeController.php
    
    public function messages($phoneNumber = null) {
        // some code here...
    }
    
    // routes.php
    Router::connect(
        '/messages/:id', // E.g. /messages/number
        array('controller' => 'messages', 'action' => 'messages'),
        array(
            // order matters since this will simply map ":id" 
            'id' => '[0-9]+'
        )
    );
    

    and you can also refer link above given by me, hope it will work for you.

    let me know if i can help you more.

    0 讨论(0)
  • 2020-12-22 08:01

    in config/routes.php

    $routes->connect('/NAME-YOU-WANT/:id',
            ['controller' => 'CONTROLLER-NAME','action'=>'ACTIOn-NAME'])->setPass(['id'])->setPatterns(['id' => '[0-9]+']
        );
    
    0 讨论(0)
  • 2020-12-22 08:01

    You can use Cake-PHP's Routing Features. Check out this page.

    0 讨论(0)
提交回复
热议问题