4

So, for weeks now I have been playing around with my own PHP MVC Framework, purely for learning purposes. I've got the basics done, and I think I know what goes where, etcetera. But I have so far not been able to figure out the following.

Say I have a music database, and I have a "music controller", a "music model" and obviously a "music view". What I want to end up doing is, being able to ofcourse insert a new artist, and/or a new track, also I would like to be able to edit them, and so on. My URL's look like this:

example.com/controller/mainfunction

In my "music controller" I have a method as follows:

public function addTrack()
{
    if (isset($_POST["submit_add_track"]))
    {
        // call the model to add track to database etc..
    }
}

So, if I were to have a form in my "music view" and submit that, I could add a new track to the database. I also have a method to add a new artist, and so on.

But here is what I am struggling with. How should I design this? Should I use the music controller to add tracks and artists? Like so:

example.com/music/add/artist
example.com/music/add/track

Or should I do it differently, maybe I should use the "music controller" just to display the artists and tracks, and insert stuff with another controller? Like so:

example.com/insert/artist
example.com/insert/track

The latter seems the way to go for me, but I am not sure if this would be good design.

I think maybe what I am asking is, would it be good to use an "insert" controller, and then a "track method" to display the "insert track" form in the view? Next to this, there will be a lot more pages, not only music. But I just cannot figure out the logic behind this on how to do this properly so I can easily extend.

As you can see, I'm lost, any pointers (big or small) would help me a lot. Thanks in advance!!

Edit: So, just to see if I'm on the right track now. I have a music controller with the code pasted below. If in the controller the "addArtist" method get's called, it goes to the model, adds the artist to the database, returns something to the controller, and it's done. Now for the part where I still have troubles. How do I display the form to actually add an artist? I know it should be in the view. But how should I call it? Something like this?

example.com/music/insertArtist

This could work, but then I would have an "insertArtist" method, for just displaying the form, and an "addArtist" method, for actually inserting the new artist to the database each time the form is submit. I know I am missing something, but I cannot quite figure this out still. What I am thinking is, in the "addArtist" method, I include the "addArtist" view file to be displayed, which holds the form, and I put in some "if submit then add the artist". Does that make sense and if so, am I on the wrong track?

public function addArtist()
{
    // $artist_model->addArtist();
}


public function editArtist($artistID)
{
    // $artist_model->editArtist();
}


public function deleteArtist($artistID)
{
    // $artist_model->deleteArtist();
}


public function addTrack()
{
    // $artist_model->addTrack();
}


public function editTrack($trackID)
{
    // $artist_model->addTrack();
}


public function deleteTrack($trackID)
{
    // $artist_model->addTrack();
}
1
  • Question. Do you plan to have a separate page for every action? The structure of your controller depends heavily on how your pages are structured. Commented Aug 5, 2014 at 18:11

2 Answers 2

3
example.com/music/addArtist
example.com/music/addTrack

You must strictly follow the format of /controller/method

When I say method, I mean literally, method. In fact, in your base controller class, the way you should call a method is

/**
 * Executes the requested action
 */
public function ExecuteMethod($method)
{
    $this->{$method}();
}

where you would pass addArtist or addTrack to the method.

Basically, the /controller/ part just tells apache which actual controller class to construct, and the /method/ tells it which method in that controller to execute.

EDIT: This is the best MVC tutorial I know of. I based my MVC knowledge largely on this. However, it is okay to variate the standard MVC structure a bit to suite your needs better, as long as you do not change the core values of MVC that make it so powerful.

EDIT 2: (reply to your edit) So what you're asking it how to view the form.

Viewing the form and submitting the form are two entirely different actions, and should be treated as such. Additionally, EVERY controller action, MUST have it's own view.

An easy way to put it, every time a user needs ANY data (or action) from the server, you MUST have an action and a view for that.

What this means, is that you will have two separate methods in your controller, viewForm() and submitForm(). Now, what you showed in your controller class, where you had addArtist(), editArtist(), etc, is inefficient, as you are just aliasing them to their corresponding model functions, which sort of skips the entire point of MVC.

I can't really tell you exactly how to implement the controller, as I do not know exactly what your goal is. But let's assume that you have a page that lists all the Artists, and you have buttons next to each for all the actions (Btw, as I said in the comments, in an implementation like this, if you are displaying Artists and Tracks on different pages, they should really have separate controllers). In this implementation, you would have a controller -> view setup like this:

  • /Artists/view -> returns an html list of artists with buttons
  • /Artists/viewAddForm -> returns a blank form you would use for creating new artists
  • /Artists/submitAdd -> returns an html confirmation page
  • /Artists/viewEditForm -> returns a form for editing with prefilled values
  • /Artists/submitAdd -> returns an html confirmation page
  • /Artists/delete -> returns an html confirmation page

So a user is presented with a list of artists. They have 3 options.

  1. They click add, fill out the form, submit it, and get taken to a confirmation page.
  2. They click edit, edit the form, submit it, and get taken to a confirmation page.
  3. They click delete, and they get taken directly to a confirmation page since there is no data to fill.

The structure for Tracks would be exactly the same.

Sign up to request clarification or add additional context in comments.

4 Comments

I had been thinking about this, but I wasn't sure why I didn't go with this. I will try this out and see if I can then see the light, and actually understand. Maybe I should take a break :) Thanks for your answer, it makes sense.
@Hammerstein and Nikzilla, I have edited my original post, just to see if I am on the right track with this.
Yes you're on the right track with the controller methods now. However, this is entirely up to you, but depending on your application, it may be more convenient to split the music controller into a separate controller for artist and track. I'll update my post with the answer to the second question.
Thanks, it makes sense and it was kinda my question in the first place. Now I should just figure out indeed how to implement this, but as long as I know what I should be working on, I'll be fine. This again helps me understand the basics of MVC a bit more again. Thank you :)
1

The way I handled this in my framework was to try and support a RESTful interface. I have

example.com/artist
example.com/track

I then use the HTTP verbs to represent the intent (POST/PUT/DELETE/GET)

It doesn't always fit neatly, but it's one way of doing it.

In your case I would worry about putting too much code in a single file. If you put all the responsibility in an insert controller, that quickly becomes an unwieldy code file. In your case I would probably go with two controllers and then create the right methods on each.

2 Comments

I also hadn't thought about this yet. I am not sure what I have been doing. Thanks for the answer, this helps me understanding it a bit more as well :)
I've been working with my framework for about 8 years now, so it takes a while.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.