Welcome Guest, Not a member yet? Register   Sign In
Function cannot call through URL
#1

[eluser]Unknown[/eluser]
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
Code:
$route['default_controller'] = 'pages/index';
$route['(:any)'] = 'pages/index/$1';

Controller:
Code:
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

[eluser]pickupman[/eluser]
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.
Code:
$route['default_controller'] = 'pages/index';
$route['pages/insertValues'] = 'pages/insertValues';
$route['pages/insertValues/(:any)'] = 'pages/insertValues/$1';
$route['(:any)'] = 'pages/index/$1';
#3

[eluser]pickupman[/eluser]
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

[eluser]Unknown[/eluser]
Thanks, this help me solved the issue.




Theme © iAndrew 2016 - Forum software by © MyBB