Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

What are the various ways of acheiving this ? Can someone please guide me?

I have to code a website for a friend (designer). The friend wants me to use core php and not any framework like codeigniter (something I really like and am comfortable with !) .

I had a look at a couple of roll your own MVC frameworks guides, but am not sure if this is the ideal approach. Since any solution I might come up with may have various bugs/security issues. My goal is to use plain php , without the help of thirdparty frameworks, to code a site , where the logic is separate from the view .

Thanks

share|improve this question
1  
Is there a reason why your friend does not want you to use any frameworks ? –  k25 Jul 16 '12 at 13:45
    

4 Answers 4

The easy method is to put all your html in a separate view folder, and just do a

include $site_path . 'views/' . $myfilename;

at the end of each code file.

How robust of a structure you set up for yourself really depends on the complexity of the project.

share|improve this answer
    
what if he needs to have variables in those templates? –  altvali Jul 17 '12 at 10:59
    
Sorry, I didn't explicitly say .php files for the views. –  Sam Dufel Jul 17 '12 at 14:47

I can understand you might want to avoid the use of some kind of 3rd party framework for continuity and/or dependency reasons, but that still leaves you with design principles to follow. In the end I would advise to your friend that it might be very beneficial for him to consider a framework which is well supported and which you are able to work with. It will likely cut back development time and catch many security holes you never even heard of.

Failing that, my guess is that you need to make a good overall design before you start coding to make sure you have at least a good overview of how you want to set up the relationships between the namespaces and the classes in those namespaces. (not necessarily in UML, it could just be a text document) Although it's almost overused in this regard, MVC is a very good pattern for separating core logic from presentation and input handling (including the security side of input handling, like SQL injection and the like).

As in any coding, security is mostly by design followed up by good coding practices. Make sure you have a good idea what the site should, and more importantly, should not be capable of so you can set constraints on what the system is capable of and what the users of the site are allowed to do.

BTW, does no frameworks also mean that you can't make use of templating engines like smarty and the like? They can help you do a lot of stuff for you and improve your security for you.

share|improve this answer
    
Thanks for replying. I am also leaning towards using a MVC pattern, but am not sure how to implement it. Could you please point me to some links ? –  gyaani_guy Jul 16 '12 at 16:23
    
these tutorials can give you an idea how to implement it practically: superdit.com/2011/10/01/… Besides that, This blog post tomdalling.com/blog/software-design/… gives a good conceptual overview. In the end MVC (or MVP) is just a bunch of classed with responsibilities properly divided between them. The Model is for data and non user related data manipulation, The Controller is for handling user input (including automated input from outsiede the system) and views are for showing the data –  Onno Jul 16 '12 at 19:18
    
part2: Pick up a pattern book if you're not up to speed on this matter. I reckon that if you're asking these kinds of questions, you need a bit more foundation and going through a book like Head First Design Patterns will really improve your overall design skills and clean up your code. (In the end the design is as least as important as the implementation of the design) I usually try to have an interface to program to, so you can make your implementation more maintainable by writing replacement classes. It also helps in testing as programming to an interface makes mocking much easier. –  Onno Jul 16 '12 at 19:28

The first question is - how big of a project is this? You say you are coding a "website" - but not what features you want. A "website" can be anything from a single page, through to a custom content management (like an image gallery), all the way to an e-commerce site and beyond. The project scope will have a definite impact on the answer to your question.

The second question - why does your friend not want to use third-party frameworks? Or is it a specific framework? Generally, frameworks are frowned on as "untrusted 3rd party" code - if this is the case, some education about where this framework is used and how it is managed could help. Another problem is the idea of "bloated" frameworks - the idea that they are stack full of functionality that will never be called on. Part of this is addressed in the scope question above, and part of this is addressed by picking a modular framework: code is only included when you need it, reducing "bloat". The last one is complexity: people can see frameworks as needlessly complicated - there is a learning curve when maintaining or extending. What they don't realise is, most frameworks are community developed (or at least, take a lot of direction from the community), and part of what the community wants is simplicity - so these frameworks get pared down over time.

So, that should address the perceived negatives of frameworks - there are also positives: community developed frameworks tend to be more secure, simply because a broader audience has reviewed and updated insecure code. Homegrown frameworks don't get anywhere near the same kind of auditing. Similarly, homegrown frameworks can easily become complex spaghetti code - again, because of this lack of broader auditing.

That all said - there are times when a framework is not necessary: a relatively simple site (say anywhere up to an image gallery or simple blog) could be written without a third-party framework. PHP Objects, Patterns and Practices discusses various patterns that can be used for "non-framework" solutions, like page controllers and so on.

share|improve this answer

The short answer is: always use a framework. Don't try to write your own (unless you do it for learning).

The long answer is more complex.

From my personal experience, I have seen that there are situations when you simply have no choice, especially when you have to work with people who are not familiar with OOP.

My suggestion is: especially if you are starting from scratch, try to convince your friend to use CodeIgniter. After all, he won't need to be an OOP or MVC guru to understand which view is loaded by x controller.

Only if you can't convince him, and if the project is very simple, write your own mini-framework, but don't try to implement a complete MVC pattern. It would be the same thing as using CodeIgniter, but instead of the original you would have a bad copy.

Here are my suggestions:

Create a 'main.php' file as a single entry point to your framework, with autoloading declaration, session initialization etc.

Create a 'classes' directory to keep all your logic separated.

Create a View class (the simplest thing you can do is including the view file and extracting the array of data to be used inside the view):

<?php
class View {

    protected $view_name, $data;

    public function __construct($view_name, $data = array()) {
        $this->view_name = $view_name;
        $this->data = $data;
    }

    //Displays view content
    public function render(){
        //Extracts vars to current view scope
        extract($this->data);

        //Includes contents
        include $this->view_name;
    }

}

Create an Input validation library.

Then, inside your pages, include 'main.php' and you will have access to all your classes and views.

<?php include_once 'main.php';
$view = new View('view.php', array('var_name' => 'value'));
$view->render();

I am writing a very simple framework which is built more or less on this schema. I wrote it exactly to deal with this kind of situations. Have a look if you need inspiration.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.