view($id) Method is being called instead of index() Method with GET VERB [CakePHP]

69 views Asked by At

When i enter URL http://localhost/cake2/cruds with GET VERB method View() loads. i want to load index() method... when i enter URL http://localhost/cake2/cruds/8 with GET VERB same happens. Remaining Routes works fine.

My Routes are:

Router::connect('/', array('[method]'=>'GET','controller' => 'Cruds', 'action' => 'index'));
Router::connect('/', array('[method]'=>'POST','controller' => 'Cruds', 'action' => 'add'));
Router::connect('/:id', array('[method]'=>'GET','controller' => 'Cruds', 'action' => 'view','id'));
Router::connect('/:id', array('[method]'=>'PUT','controller' => 'Cruds', 'action' => 'edit','id'));
Router::connect('/:id', array('[method]'=>'DELETE','controller' => 'Cruds', 'action' => 'delete','id'));

Same Routes works fine in CakePHP v3. My Controller Methods are:

function index()
        {
            $this->loadModel("crud");
            $users = $this->crud->find('all');
            //var_dump($users);
            $users = Set::extract($users, '{n}.crud');
            $this->set('message', json_encode($users,JSON_PRETTY_PRINT));  
        }`              

`function view($id)
        {
            $this->loadModel("crud");
            $user = $this->crud->findById($id);
            if (!$user) {
                $this->set('message', "User Not Found..!");
            }
            else
            {
        $this->set('message',json_encode($user['crud'],JSON_PRETTY_PRINT)); 
            }
        }
1

There are 1 answers

0
ɹɐqʞɐ zoɹǝɟ On BEST ANSWER

this line clearly says that it will call view action on id

Router::connect('/:id', array('[method]'=>'GET','controller' => 'Cruds', 'action' => 'view','id'));

so as you want to call it index so we added index instead of view

Router::connect('/:id', array('[method]'=>'GET','controller' => 'Cruds', 'action' => 'index','id'));

(& i dont know cake php)