ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Function cannot call through URL

December 05, 2012 4:14pm

Subscribe [1]
  • #1 / Dec 05, 2012 4:14pm

    conmen80

    1 posts

    I’m a fresh learner in using CI, below is a controller with function insertValues() that I try to call directly in URL, which looks like http://sitename/codeIgniter/pages/insertValues, but it returned 404 Page Not Found when its call, after few searched in google, I found that is an issue of route.php which I do not know how to configure it.

    Please refer to below route.php, the function insertValues() will work only if remove $route[’(:any)’] = ‘pages/index/$1’;, but somehow once I call page like http://sitename/codeIgniter/home, it will show 404 Page Not Found.

    route.php

    $route['default_controller'] = 'pages/index';
    $route['(:any)'] = 'pages/index/$1';

    Controller:

    class Pages extends CI_Controller{
    
    public function view($page='home'){
    
        if(!file_exists('application/views/pages/'.$page.'.php')){
            show_404(); 
        }
    
        $data['title'] = ucfirst($page);
    
        $this->load->helper('url');
        $this->load->model('getdb');
    
        $data['results'] = $this->getdb->getAll();
    
        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);
        //$this->insertValues();
    
    }
    
    
    function insertValues(){
        $this->load->model('getdb');
    
        $newRow = array(
            'name' => 'andy'
        );
    
        $this->getdb->insert1($newRow);
        echo "inserted!";
    }
    
    }

    Please help in advice, many thanks.

  • #2 / Dec 05, 2012 4:39pm

    pickupman

    1317 posts

    You should add another route for that method. By using (:any) as a route, you will need to explicitly write every other controller/method as a route for it to work.

    $route['default_controller'] = 'pages/index';
    $route['pages/insertValues'] = 'pages/insertValues';
    $route['pages/insertValues/(:any)'] = 'pages/insertValues/$1';
    $route['(:any)'] = 'pages/index/$1';
  • #3 / Dec 05, 2012 4:41pm

    pickupman

    1317 posts

    If you plan on having other controllers, you also find it helpful to at least have the pages segment in your URL. Then you wouldn’t have to use the (:any) route. You could use ‘pages/(:any)’.

  • #4 / Dec 06, 2012 5:55am

    conmen80

    1 posts

    Thanks, this help me solved the issue.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases