PHP Mini-Framework (Need Suggestions)

james-geiger

Sep 10th, 2016 04:28 PM

Hi,

I just joined after viewing your tutoiral on YouTube about a registration/login site done in PHP. I am new to PHP so I do not want to learn MVC or Laravel at this time. I am focusing on core PHP and understanding everything first and thus, am writing my own min-framework based on your tutorial.

My progress is here, but can you provide a little guidance on what my next steps should be? Should I:

  • Create a user.class.php that contains the user logic and all of that?
  • A session class?
  • A router class?

I just want the basics and want the challange of writing my own, but can you provide some hints in the right directon on the minumum features I'll need for this small framework?

Thanks!

tnylea

Sep 14th, 2016 02:52 AM

Hey James,

I think that's really cool that you want to build a mini-framework. As far as the features that you will need I think that kind of depends on how big you want the mini-framework to be, but here are some typical functionalities that are provided with a framework:

  • Routing Functionality
  • Database/Active Record implementation or Helper
  • Session/Cookie Helper Class

I think the next step would definitely be a way to add Routing to your application. Some good ways to look at how to write a routing class might be to look at how some of the popular frameworks, such as CodeIgniter or Laravel do it.

You could also take a look at the following code on how you might do a simple router:

<?php
// ******************** //
// SETUP THE ROUTER
// ******************** //
// Get the current request URI
$uri = $_SERVER['REQUEST_URI'];
// Trim off any additional forward slashes
$uri = trim( $uri, "/" );
// Convert the URI parameters into an array
$url = explode('/', $uri);
// Set the default file to the home page
$file = 'home';
// if the first parameter exists
if(!empty($url[0])):
	// Set the file to be the first parameter
	$file = $url[0];
endif;
// Create arguments and set it to the url parameters except the first
array_shift($url);
$args = $url;
<p>// LOAD THE VIEW
if(file_exists($file . '.php')){
require $file . '.php';
} else {
require '404.php';
}

Hope that helps and let me know if you have any questions about the code above :)

Thanks again. I'm sure I could help you contribute a little to the mini-framework.

Looking forward to hearing from you soon.