panique / mini

Just an extremely simple naked PHP application, useful for small projects and quick prototypes. Some might call it a micro framework :)
1.35k stars 478 forks source link

[Question] How to display one song? #232

Open eldinor opened 7 years ago

eldinor commented 7 years ago

First of all, great thanks for this project - it helps a lot to know about MVC for such beginners as I am.

Maybe this question is too simple, but my current knowledge of PHP doesn't let to solve it by myself. So, if you call http://example.com/mini/songs/ the script calls the index method of the songs controller. What should I do to get the page like http://example.com/mini/songs/1 (where song with id=1 will be displayed)? I suppose that in this case I need another method in the songs controller. But how to pass the id to this method? Probably it should be done in core/application.php? I will be very grateful for the answers. Pieces of code are welcome :)

bravedave commented 7 years ago

adjusted index

public function index( $songID = 0)
    {
         if ( ( $songID = (int)$songID) > 0) {
             // then $songID must not be zero
            // so tell them all about song number one - something like
            $song = $this->model->getSong( $songID);
            // load views. within the views we can echo out $songs and $amount_of_songs easily
            require APP . 'view/_templates/header.php';
            require APP . 'view/songs/songview.php';
            require APP . 'view/_templates/footer.php';

         }
         else {
            // getting all songs and amount of songs
            $songs = $this->model->getAllSongs();
            $amount_of_songs = $this->model->getAmountOfSongs();
            // load views. within the views we can echo out $songs and $amount_of_songs easily
            require APP . 'view/_templates/header.php';
            require APP . 'view/songs/index.php';
            require APP . 'view/_templates/footer.php';
          }
   }
eldinor commented 7 years ago

Thanks a lot! But everything what I've got is the Problem page... Seems that song ID is detected as action but doesn't have proper method. Below there is data from debug mode.

http://localhost/mini/songs/deletesong/3 Controller: songs Action: deletesong Parameters: Array ( [0] => 3 )

http://localhost/mini/songs/4 Controller: songs Action: 4 Parameters: Array ( )

bravedave commented 7 years ago

I think your right there - you have to go to a function - so like the deletesong example i.e. http://localhost/mini/songs/viewsong/3