phpcodeignitergrocery-crud

Listing okay, but all other functions are not working


I'm totally new to Codeigniter and Grocery CRUD, but I very like the concept, so I'm trying to make my little app with these frameworks.

So, right now the login system is working and after login on the "home" page, I see my Grocery CRUD table, it's filled, so far it's okay...

My problem is, that I can't use any function: add, edit, view, search is not working, it gives me 404 error for example this is one edit link:

http://subdomain.mysite.com/index.php/home/edit/1

The site is on a subdomain, I don't know if could be relevant or not.

This is my base_url:

$config['base_url'] = '/';

If I change this to '', the base_url(); method doesn't gives me the right value, I assume it's because the subdomain.

This is my complete "Home" view:

    <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>My system</title>
    <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <h1 class="page-header text-center">My system</h1>
    <div class="row">
    <div class="container text-right">
            <?php
                $user = $this->session->userdata('user');
                extract($user);
            ?>
        <p class="d-inline"><b>Logged in as:</b> <i><?php echo $fname; ?></i></p>
    </div>
        <div class="container">
            <?php 
            $crud = new grocery_CRUD();
            $this->grocery_crud->set_table('mobil_table');
            $this->grocery_crud->columns('mobile_number', 'package_name', 'package_date');

            $output = $this->grocery_crud->render();
            $this->load->view('mobil.php', $output); ?>
            <a href="<?php echo base_url(); ?>index.php/user/logout" class="btn btn-danger">Logout</a>
        </div>
    </div>
</div>
</body>
</html>

My routes:

    $route['default_controller'] = 'user';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

$route['home'] = 'user/home';

What should I correct to make these functions work on this page? Thx :)


Solution

  • You must access your edit page via your controller/method manner

    if your controller is user and it has a function edit with a param name $id, your accessing url will be:

    http://subdomain.mysite.com/index.php/user/edit/1
    

    or define new route for it:

    $route['home/edit'] = 'user/edit';
    

    or more clearly as

    $route['home/edit/(:num)'] = 'user/edit/$1';
    

    But you can get clean urls by following these steps:

    First create a .htaccess file in your websites root folder with this code:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
    

    After that change:

    $config['base_url'] = 'http://subdomain.mysite.com/';
    

    and

    $config['index_page'] = '';
    

    After doing the above steps, you can access your urls without index.php in urls

    for example:

    http://subdomain.mysite.com/index.php/user/edit/1
    

    will become

    http://subdomain.mysite.com/user/edit/1