1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Make MVC PHP from Scratch

Discussion in 'PHP' started by huseinbandi, Jul 31, 2015.

  1. #1
    hi developers,

    Do you've made web based on mvc using php? from sratch? please share the tutorials here.
     
    huseinbandi, Jul 31, 2015 IP
  2. freelanceDeveloper

    freelanceDeveloper Member

    Messages:
    59
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    43
    #2
    not 100% understanding your question but think of mvc as

    ->dividing your scripts in 3 files , each located in a seperate folder.
    M = model -> datalayer
    V = view -> html code
    C = controller -> php code

    then , you have a 4th script, index.php fe. which calls the controller , the controller performs some actions, calls the data layer if needed and returns the result to the view to handle the display...

    Most often php's autoload() function is used so no manual includes() are required, but doing it manually as a poc, will make you understand the process.

    You can find many simple examples googling "simple php mvc tutorial" or similar terms.

    I remember it took me some time to understand the benefits of this, but once you do you will always go the mvc way. It makes life much easier when it comes to debugging etc
     
    Last edited: Jul 31, 2015
    freelanceDeveloper, Jul 31, 2015 IP
  3. Helge Sverre

    Helge Sverre Prominent Member Affiliate Manager

    Messages:
    840
    Likes Received:
    99
    Best Answers:
    2
    Trophy Points:
    305
    Digital Goods:
    2
    #3
    Take a look at this video series from JREAM:
     
    Helge Sverre, Aug 12, 2015 IP
  4. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #4
    Oh Jesus...I got 5 minutes in and saw HUGE security flaws.. I then continued to watch and for everything he added I could see several problems/insecurities and foreseeable future development problems.

    While "rolling your own" sounds like a lightweight speedy solution it's hardly ever productive. You are way better off using an existing mature Framework like Laravel or Zend if you need MVC.

    That video isn't worth watching. It will just give you insight on how to make a piece of shit Framework that will ultimately waste your time when you launch in production.
     
    NetStar, Aug 14, 2015 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    Not to mention that creating an MVC for PHP is quite pointless to begin with. Since the code will be interspersed PHP/html in quite a few iterations, having a logical structure of your site, proper classes etc. can work just as well as an MVC setup.
     
    PoPSiCLe, Aug 14, 2015 IP
    deathshadow likes this.
  6. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #6
    That's a contradictory statement... "creating an MVC for PHP is quite pointless" and "can work just as well as an MVC setup"...

    While PHP doesn't offer a *true to the name* MVC style the pattern of separating Presentation from Logic from Configuration from Data Handling is very much so useful in any language. MVC for PHP does just that. So it's absolutely a good idea.... I do however agree with you that logical strucure and proper classes can work just as well... Personally I can't recommend "rolling your own" since you will spend a lot of time reinventing what has already been tried and tested.
     
    NetStar, Aug 15, 2015 IP
    deathshadow likes this.
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #7
    I think he meant to say BETTER, not "just as well"

    That's the thing, to me what should be done in PHP -- which by its very nature is a top-down execution language -- is a separation of output from data handling, with isolation of scope.

    Whilst that separation has similarities to MVC it most decidedly IS NOT the same thing; MVC is designed for event driven programming something that PHP most most decidedly not -- this is why attempts to shoe-horn MVC "paradigms" (I hate using that word, but it's the one the Ivy league ****'s who know **** about **** use for it) into PHP amount to little more than the equivalent of shoving a fat woman's size 14 hoof into a size 7 shoe; even if you pull it off it's sure as hell not gonna walk very far.

    It's why generally speaking MVC is a fat bloated steaming pile of developer ineptitude laced with "objects for nothing", damned near trying to implement an entire language in what's already and interpreted environment, and worst of all wasting time recreating existing functionality!

    Admittedly, wasting time recreating existing functionality seems to be a common problem amongst many people out there making websites with PHP -- see the outright idiotic halfwit mouth-breathing dumbass bull known as "template systems" like Smarty, when PHP itself already is a templating system!

    As such I would not call crapping all over PHP with "MVC" a "good idea" -- the out of order execution event driven "constant state" concepts on which it is based makes perfect sense when dealing with window toolkits from languages like C#, Java or C++ -- it makes little to no sense when there's only ONE request, ONE output, and linear driven execution.

    That people even TRY to use that idiocy is IMHO just more blindly parroting and copying what instructors and "professional educators" not qualified to be teaching jack **** are telling them; sadly a state of affairs that seems to get worse and worse year after year.

    But of course, such fashionable code bloat is so much so "the norm" right now, that it's become an epidemic at every level of site production which is why you'll constantly see posts where people ask "what's wrong with my site", "why is my site so slow", "why is Google penalizing my site", "why is my hosting overtaxed", "why am I being kicked off shared hosting" when the page is built from fat bloated train wrecks of how not to write PHP, fat bloated train wrecks of how not to write HTML, and endless pointless off the shelf libraries that basically piss all over the resulting website! ... and that's BEFORE we mention the art faygelah asshattery.

    See all the ignorant fools who see nothing wrong with wasting half a megabyte of scripttardery, quarter of a megabyte of CSS, 100k of HTML and 3 megabytes of images on delivering 1k of plaintext and a half dozen content images that shouldn't even break 256k TOTAL for "all of the above".

    Pathetically the 10k of HTML for every 1k of markup seeming to be a hallmark of back end developers less qualified to be writing PHP or ASP than the PSD jockeys are at designing an accessible front-end. See the nimrods, nitwits and shmucks who seem to claim to know PHP but not know enough HTML to use PHP properly... They'll even say "Well I know PHP but I don't know enough HTML, we have a front end guy for that." -- HERPAFREAKINGDERP, how does that even HAPPEN!?!
     
    Last edited: Aug 15, 2015
    deathshadow, Aug 15, 2015 IP
    PoPSiCLe likes this.
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    I've never really understood the whole MVC for PHP. Every time I look at some MVC concept, it just looks like an unnecessary bloat of files and folders, just for dividing up the functionality in different files. When you end up having 25 includes, or 20 files with less than 5 lines of code... I don't see the point.
     
    PoPSiCLe, Aug 16, 2015 IP
    deathshadow likes this.
  9. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #9
    I see the point, to learn the app, its more easy. As the 25 includes all got differend names for their differend functions. Problems with a function is easy, you alter only that file and all is done! :)
    the MVC concept is great, i use it for most of my sites as it gives me way more options and time to code the site instead of writing classes and such... as they are already writen! :)
     
    EricBruggema, Aug 17, 2015 IP
  10. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #10
    Yes, but I don't really see how it's beneficial - if you have say 20 different functions you use around the site, in different locations, you can either load the separate files with separate functions when you need them, or you can just do a "require_once('functions.php')" at the beginning of whatever loader you're using. Will that mean you'll be including PHP-content you will not be using on every page? Definitely. Will this affect anything? Not really, and the functions will be available if you need them, without having to remember that that exact one isn't loaded yet. Granted, when you start getting into thousands of files, maybe it makes a bit more sense to have separation of functionality so that you don't have to load huge libraries that's not being used, but if not, I don't really see the benefit.
     
    PoPSiCLe, Aug 18, 2015 IP
  11. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #11
    That's my problem with it -- they make the wild claim that it's going to be faster for not loading code that isn't used, but then use ten times as much code and go to the filesystem more completely offsetting that advantage; gets worse when half these damned libraries (codeignitor comes to mind) waste time replicating existing functionality! It's bad enough when people endlessly brute-force things that "PHP, yeah, we have an existing function for that" without a programming model that once shoe-horned into PHP seems DESIGNED to encourage that...


    Broken Engrish moist goodry aside -- Every time I see claims like like that, particularly the use of the word "easy" I start to think I have a radically different definition of that word from others; basically every time I see people say things like "MVC makes it easy" or "jQuery makes it easy" or "bootstrap makes it easy", well...

    [​IMG]

    It gets worse when there's things like this:

    Assuming you mean different (differend isn't even a word) that would be semi-acceptable if said functions were actually WRAPPED in functions, but far too often you see programmers blindly slapping included files in INSTEAD of functions or methods -- or making it load without _once (because some dumbass told them it's faster -- it's only faster if you know you're only doing the include once), and in general just creating more entry vectors.

    Though it seems most of the halfwits morons and fools who fall for MVC on PHP and it's outright lies on efficiency and "ease" are completely ignorant of things like restricting scope, why one should leverage setters and getters (a concept PHP wouldn't even need if it had a local access global read only property type) or even the simple fact that if a file is NOT supposed to be called by the user directly, EVERYTHING in it should be wrapped in a class or function so that it doesn't output jack ****.

    But in many ways I think much of that can be blamed on PHP's "insecure by design" methodology, where ridiculously simple changes to what can and cannot be done by certain functions would make 99% of the PHP cracks of the past decade and a half fall flat on their face. Simple things like not letting normal file operations (fopen, readfile) be able to even TOUCH .php files for read or write, along with= not letting include/require load any file that doesn't have a .php extension. Simple things like adding to the existing public/protected/private model a readonly public type where only the object itself can write to the property. (removing the need for getters).

    Existing MVC frameworks, and even the concept of MVC falls into the same trap as other frameworks -- it lets people not qualified to be building websites sleaze together off the shelf parts in a bloated mess, blindly hoping that the person who made the framework actually knows more than you or that it will magically provide security or good practices. TYPICALLY the exact opposite is the result. It's why they end up sitting around scratching their heads as to why people complain that it's slow, bounce in droves, get kicked off of hosting for hogging too many resources, and drowning in the deep end of the pool the moment they want to do something that "framework" fails to provide

    But as you all know, I say the exact same things about jQuery, bootstrap, YUI, blueprint, prototype, and just about every other SLEAZY SHORTCUT out there that to be frank, aren't actually shortcuts. It's like people seem to intentionally be trying to make things harder or trying to fail from the start either out of ignorance, or blindly believing propaganda that has zero basis in reality.

    Hence why talking to fans of things like Codeignitor or Laravel (or turdpress, or jquery, or whatever the current flavor of the week snake oil is) ends up like talking to cultists; and why talking to the people behind said systems is like having a sit-down with David Koresh, Jim Jones or Marshal Applewhite. It all reeks of cognitive dissonance, confirmation bias, and a woeful lack of understanding the most basic of things one should know before even ATTEMPTING to build a website.

    Which is why as I keep saying, as a USER of websites I'm generally finding them less useful and slower on todays broadband than they were two decades ago on dialup. Scripttardery, crappy back-ends and code bloat dragging performance into the annals of hell, woefull ignorance of accessibility norms... It's pretty much like most "designers", "scripters" and "back end coders" have teamed up to give a giant double **** you to people actually USING websites!

    But again I suspect much of that is just the halfwit "credit mentality" in action; pay more later for something you can't afford know... We want it now, damn the consequences -- such a solid plan for success.
     
    deathshadow, Aug 18, 2015 IP
  12. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #12
    I still think you are wrong. Most MVC's have lots of bloat, stuff you don't need.. as my own writen mvc only contains stuff i need for the site i'm working on. Adding / removing classes and functions are as easy as copying a file to a directory as the MVC will load it when needed. As for the way a MVC works, when creating/building your own MVC you'll get a better understanding of how PHP / Websites work, and will help you develop sites in the future way faster as you don't need to rewrite all the code again and again. Other good point is that the index.php (the controller) will handle everything, and i meen everything. So no more 404 pages of non existing scripts/pages. Error handler that handles the 'whole' site instead of just a script file and such.

    As you point out that all libraries are bloated, i agree (jQuery, bootstrap and such) contain way and way more then you'll ever need, same as PHP, you never use all functions... or to go way higher, you don't even use 10% of your OS files as the rest is there to help when you possibly need them.
    Same counts for libraries, they are ment for the world and could possibly do everything a user wants. As for the speed, in this time a 10k or 100k js file won't make a big difference on a normal tablet/desktop when you check the speed. As for PHP, the less code, the faster the script so my MVC is way faster then other MVC models as i only have coded what i think i need, and when i need more, i just create a new class or function to make use off.

    btw sorry for my poor writen english, i'm trying to do my best but languages arn't my favorites to learn (and remember). But i know you all understand my writing :)
     
    EricBruggema, Aug 19, 2015 IP
  13. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #13
    Which really doesn't have much to do with MVC...

    Given that MVC's modal state event driven nature is the antithesis of how PHP is supposed to work or how accessible sites work, I REALLY don't see that at all.

    Which again doesn't necessarily have anything to do with "MVC".

    You do know that even if you have a "one index to rule them all" you should STILL be sending a 404 header, right? Not sure what you even mean by that but it sounds WAY wrong.

    Until you have five or ten of them or more than 30 people visiting a site simultaneously. Remember it's not just about client speed, but also server load.

    ... and really if you think that 10:1 is easier, faster or won't make a "big difference" you must live in one of the magic worlds where everyone has low latency broadband -- a relative myth across most of the English speaking world. What with our friends in Canada facing bandwidth caps with price penalties for overages, tethered and phone plans containing same if you're more than 40 miles from a state capitol, or our friends in Australia and New Zealand who end up being dropped to shotgun dialup speeds if they go over their limits.

    Concepts lost on the fashionable effete inner city elite or those who came to the party late enough to not be tied to legacy infrastructure for the coming decade or more like the rest of us.

    Makes me wonder if you actually know what MVC is -- it sounds like you are using a few of the concepts and maintaining some separation, but not actually following the MVC model.

    From what you've said in that post, my "poor man's" setup would qualify as MVC when it most decidedly is NOT.
     
    deathshadow, Aug 19, 2015 IP