Welcome Guest, Not a member yet? Register   Sign In
MVC basics - how do I pass something from URL to model?
#1

[eluser]kosmosik[/eluser]
I am building simple application.

I have a page (/controler/view/id) which shows data of selected ID (id is in URL). How do I pass that ID to model function (show_id f.e.) returning the record for given ID?
#2

[eluser]thunder uk[/eluser]
You can get the id from the url by using the URI class like this:

$id = (int)$this->uri->segment(3);

See http://www.ellislab.com/codeigniter/user...s/uri.html for more info
#3

[eluser]kosmosik[/eluser]
I know how to get this variable. I just don't know how to pass it around in MVC.

Fe. I have sample controller (mind the comments):
users.php
Code:
class Users extends Controller {
    function index()
    {
        // code ommited
    }
    function view($user)
    {
        // something - what?
        $this->load->view('view_view',$data);
    }

I have a model:
Users_model.php
Code:
class Users_model extends Model {
    function Users_model()
    {
        parent::Model();
    }
    function list_users()
    {
        // code ommited - this is obvious, here I return all users
        // found in DB table
    }
    function view_user()
    {
        // what here? how the code should look if I want to return to
        // controller simple SELECT statement that returns me one row
        // WHERE the user is /users/view/{username}?
    }
}

How do I do this? There is much about models/views/controllers in the guide but there is very little about how to connect them together.
#4

[eluser]Ainab[/eluser]
this may help you.
Code:
function index(){
    $id = (int)$this->uri->segment(3);

    // this is your model function
    $returnedVAlue = $this->your_model->your_function($id);
    $data['returnedVAlue'] =  $retrunedVAlue;
}
#5

[eluser]ztinger[/eluser]
thunder uk already answered your question.

you can use $this->uri->segment() in either your controller or model.


Code:
<?php
    function view_user()
    {
   $id = (int)$this->uri->segment(3);
   //.. rest of your query goes here...  
  
    }
?>
#6

[eluser]kosmosik[/eluser]
But this is not too much MVCish. I mean you hardcode the URL segment into model. So you don't really abstract the application. When you change something in view (URL f.e.), routing or controller you also need to change model.

Isn't the point of MVC to abstract these layers from themselves?

Isn't there (there must be) any other method to pass this stuff from controller into model?
#7

[eluser]ztinger[/eluser]
Of course, like I said, 'you can use $this->uri->segment() in either your controller or model.'

In your controller

Code:
<?php

$this->load->model('users_model');


$id = (int)$this->uri->segment(3);

$data['profile'] =$this->users_model->view_user($id);

//process data['profile'] if you need to
// then..

$this->load->view('view_view',$data);

//btw: there is no need to name your models 'something'_model or your views 'something- view...


?>



in your model:

Code:
<?php
<?php
    function view_user($id)
    {
  
   //.. rest of your query goes here...  
  
    }
?>
?>

This is a very basic question. Just re-read the user guide, and watch the video tutorials so you grab the concept better.
#8

[eluser]kosmosik[/eluser]
Thanks I'll try that.

[quote author="ztinger" date="1183069547"]
Code:
//btw: there is no need to name your models 'something'_model or your views 'something- view...
[/quote]

That is for my editor. It shows only filename on it's tabs so I name the files this way so I know which one (on tabs) do what. Smile
#9

[eluser]kgill[/eluser]
If I may offer an alternate suggestion here, pass parameters instead of hard coding the uri segment. You are already getting $user in your controller just use it.

So in your controller:

Code:
function view($user)
    {
        // something - what?
        $this->load->model('Users_model');
        $somevariable = $this->Users_model->view_user($user)

        $this->load->view('view_view',$data);
    }

Then just change the view_user function in your model to take a parameter:

Code:
function view_user($user)
    {
        // what here? how the code should look if I want to return to
        // controller simple SELECT statement that returns me one row
        // WHERE the user is /users/view/{username}?

        // in here use $user in your where clause by bind variables, string concatenation, whatever
    }

- K
#10

[eluser]thunder uk[/eluser]
[quote author="kosmosik" date="1183068925"]But this is not too much MVCish. I mean you hardcode the URL segment into model. So you don't really abstract the application. When you change something in view (URL f.e.), routing or controller you also need to change model.

Isn't the point of MVC to abstract these layers from themselves?

Isn't there (there must be) any other method to pass this stuff from controller into model?[/quote]

I must admit, I do prefer to have my models work as 'black boxes'. Internally, they know nothing of the application outside. From the outside, they show only the methods that 'do things' and when you call those methods, for example from a controller, you provide them with the extra bits of info that they need.

In this case, if I had a user model that performs a database lookup for a particular user, I'd provide the user model's getUserInfo method with the userid and get back an array for that user.

You put your identifier in, you get your info out.




Theme © iAndrew 2016 - Forum software by © MyBB