Welcome Guest, Not a member yet? Register   Sign In
URI Issue
#1

[eluser]robert.fulcher[/eluser]
I am new to CI. I have done some work in CakePHP a few years ago as well as Spring MVC for Java. So I understand most things just not how to do them in CI. At this point I am trying to pass data through the uri for the deletion of a record. I keep getting 404 page not found. I have other links on pages, this is the first with added data to the end.

if I do /index.php/vendor_del it sees something and give me the sql error that I need an id.

if I do /index.php/vendor_del/501 I get the 404 page not found.

Any suggestions on how to fix this, or areas to find examples. I have not found a good CRUD application example to go from.

Thanks (I am liking it more than I did cakephp)
#2

[eluser]Armchair Samurai[/eluser]
A little more information would help - can you post your controller? Are you using CI's routing function?
#3

[eluser]Christopher Blankenship[/eluser]
With what you typed you are creating a controller called vendor_del with a method of 501. Unless you have routes working another way.

/index.php/controller/method/variable
such as
/index.php/vendor/vendor_del/501

This way you would have a vendor controller a function method called vendor_del being passed the variable 501 using the uri.

Vendor Controller
Code:
<?php
class Vendor extends Controller
{

   function Vendor()
   {
      parent::Controller();
   }

   function vendor_del()
   {
      $id = $this->uri->segment(3, 0);
      if ($id == 0)
         echo "Error No Id passed!";
      else
         echo $id; // do something with the $id;
   }

}
?>
#4

[eluser]robert.fulcher[/eluser]
Here is my controller. I have not changed any routing so I am assuming it is the default.

vendor_del.php (inside a vendor folder under controller folder)

Code:
<?php

class Vendor_del extends Controller{
   function index()
   {
      $this->load->model('Vendor_model','',TRUE);
      $this->Vendor_model->vendor_del();
      $redirect("vendor/vendors");
   }
  

}
?>
#5

[eluser]robert.fulcher[/eluser]
weremax.

The controller is in a folder called vendor.
#6

[eluser]robert.fulcher[/eluser]
I just realized that I was using the index() function in the vendor_del class. I changed it to vendor_del and it works. Can anyone point me to the list of defult functions such as index(), submit()... that can be used in a controller?

Thanks very much for everything.
#7

[eluser]Christopher Blankenship[/eluser]
Okay you figured that out before I was done typing my message. for your other request I would suggest the following Codeigniter User Guide it has alot of content as well as the Controller information but also Reserved Names and is well documented.
#8

[eluser]robert.fulcher[/eluser]
I got an email of a post that I should put all the functions for vendor in the same controller. I also read a post say that different funtions should be seperated out into there own controllers. What is the best practice?

I would prefer one controller but how do you hande an add and an edit with both having a submit function. Now I would have a vendor_add with a submit and a vendor_edit with a submit.....

Thanks
#9

[eluser]Christopher Blankenship[/eluser]
I group items in Controllers, and Models with separated views. Below is an example only and is a quick rough draft.. As you see below though I separate the logic for database access into a model for vendors. there are multiple views.

These are vendors so I have a Vendor.php in the Controller folder. A Vendor_model.php in the models folder for database access to the vendors table for editing, adding, and even retrieving records. In the view folder I would have vendor_list.php, vendor_edit.php, vendor_add.php, and vendor_error.php


Code:
<?php
class Vendor extends Controller
{

   function Vendor()
   {
      parent::Controller();
   }

   // DELETE
   function vendor_del()
   {
      $id = $this->uri->segment(3, 0);
      if ($id == 0)
         echo "Error No Id passed!";
      else
         echo $id; // do something with the $id;
   }

   // ADD
   function vendor_add()
   {
      if ($_POST) {
         $array['id'] = 0;
         $array['name'] = $this->input->post('string');
         $array['name'] = $this->input->post('string');
         $this->load->model('Vendor_model', 'vendor', TRUE);
         $error = $this->vendor->insert($array);
         if (!$error)
            $this->load->view('vendor_list');
         else
            $this->load->view('vendor_error');
      }
      else {
         $this->load->view('vendor_add');
      }
   }

   // EDIT
   function vendor_edit()
   {
      $data['id'] = $this->uri->segment(3, 0);
      if ($_POST) {
         $array['name'] = $this->input->post('string');
         $array['name'] = $this->input->post('string');
         $this->load->model('Vendor_model', 'vendor', TRUE);
         $error = $this->vendor->edit($array, $data['id']);
         if (!$error)
            $this->load->view('vendor_list');
         else
            $this->load->view('vendor_error');
      }
      else {
         $this->load->view('vendor_edit', $data);
      }
   }
}
?>
#10

[eluser]obiron2[/eluser]
And you are of course going to add some validation to your controller to ensure the user is logged in and has the correct priveleges to delete that specific record BEFORE you call the model function.

If you don't then a spider (or a malicious hacker) could come along and simply delete all of your data

Obiron




Theme © iAndrew 2016 - Forum software by © MyBB