Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Php converting an existing project to mvc

Options
  • 23-01-2013 1:32pm
    #1
    Registered Users Posts: 1,551 ✭✭✭


    I'm trying to write an existing php web project in mvc because I thought I might learn something by doing it.
    The project is currently a bit all over the place, doesn't have classes for the different objects and is a bit hard to follow so I'm trying to group it into classes and build it all into a model view controller pattern.
    I'm just a bit confused as to where everything goes, its a bit of a headwrecker for a php beginner like me.
    I'm wondering where do the isset statements checking for get and posts go?
    Should they all go in the controller class?
    Should all the methods be created in the model classes?
    Can I call other classes in the controller class and access their methods there via an instance of the class within the controller class?
    Can I have if statements in my view part ie. the php pages or is that defeating the purpose of mvc?
    Please help I'm confused.


Comments

  • Registered Users Posts: 241 ✭✭fcrossen


    Depending on how complex the application is that you are trying to migrate, you might want to start with a simpler example.

    Are you using a MVC framework? I suggest starting with a simple example (for example user management) and getting to grips with MVC, your chosen framework (if any) and PHP in general.

    If you are a beginner, you are trying to:
    - learn MVC
    - learn a framework
    - learn PHP
    - refactor someone else's (possibly sucky) code

    A lot to do there!
    quinnd6 wrote: »
    I'm wondering where do the isset statements checking for get and posts go?
    Should they all go in the controller class?
    Generally yes. This is part of the business logic i.e. checking permissions, what posts to display, etc.
    quinnd6 wrote: »
    Should all the methods be created in the model classes?
    Models interface between your persistent store (generally your database) and your logical view. For example the logical view of a user contains group and permissions data, but this data is usually stored in separate tables in the database. Your model should be concerned with storing and retrieving data from your data store.
    quinnd6 wrote: »
    Can I call other classes in the controller class and access their methods there via an instance of the class within the controller class?
    Absolutely. For example a controller class for 'users' would instantiate a model class called 'users'.[/QUOTE]
    quinnd6 wrote: »
    Can I have if statements in my view part ie. the php pages or is that defeating the purpose of mvc?
    "That would be an ecumenical matter"
    Some people would say "never", but I do. However only put in logic that is related to the view file. For example:
    [PHP]<?php if( $user->is_authorised() ): ?>
    <!-- show this stuff -->
    <?php else: ?>
    <!-- show other stuff -->
    <?php endif; ?>[/PHP]
    Your view file should never contain code that can be contained in the controller or the model.


  • Registered Users Posts: 1,551 ✭✭✭quinnd6


    Thanks for the info.
    I hadn't intended on using a framework but maybe it would make things a bit easier.
    Have you any suggestions of a good framework to use with mvc?


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    quinnd6 wrote: »
    Thanks for the info.
    I hadn't intended on using a framework but maybe it would make things a bit easier.
    Have you any suggestions of a good framework to use with mvc?

    CakePHP and Symphony2 are two that I've heard make PHP less than completely ****ing horrible to use.

    Disclaimer: I, thankfully, am not a PHP developer. Your mileage may vary, always make sure to check the label.


  • Registered Users Posts: 241 ✭✭fcrossen


    I'm not fond of Symphony2. CakePHP is OK.

    Do look at:
    - http://ellislab.com/codeigniter
    - http://agiletoolkit.org/


  • Registered Users Posts: 26,571 ✭✭✭✭Creamy Goodness


    I've heard good things about http://laravel.com/ but it's still quite young.

    http://lithify.me/ is written by former CakePHP guy(s) and is quite good.

    Symphony2, Zend, CodeIgniter and CakePHP seem to be the most popular from talking to people and stackoverflow.com


  • Advertisement
Advertisement