Create your own PHP framework using MVC design pattern (Part – 2)

Ok!, at last I have finished creating my own MVC PHP framework. I named is Sp!ce (spice). I think you guys will get lot’s of fun using this framework. So let’s start.

If you didn’t read Part-1 then please do that before continue.

Step 1: Folder Structure

I think folder structure is really important. So we need to fix that first. In my opinion, ROOT folder need to settle down with these following folders

spice-framework

Step 2: We are now create the .htaccess file. In this file we will use `mod_rewrite.c` apache module and need to prevent file browsing option by using “Options -Indexes”. So our .htaccess file should be look like this

click here to see .htaccess file

Step 2: Now we need to create our index.php file. In this file all we need to load controller, model and view classes and off course configuration and start up file.

click here to see index.php file

Step 3: Now you can see I have created a folder system because this folder will collect all the system files like config.php file along with model, view, controller classes that I have created. startup.php  is holding a function that will check every URL that pass through by browser and let the system know which controller will need to fetch and pass. Before digging more into that we will need to know more about the config.php file. We need to set the database related global variables like database host name, user name, password and the server host.

click here to see config.php file

Step 4: Our last goal is to create a startup.php file. This is the file where each and every URL that need to pass to the browser via index.php file. This file will check whether it goes to custom controller or default controller.

click to see startup.php file

Step 5: Download the full source code. I have created a small project named ‘addressbook‘ by using spice framework. There is also a mysql database dump file. You need to  process these follow these steps:

click to download full source code

1. Go to phpmyadmin and create a database named `spice`;
2. Now import the `sql/spice.sql` file to the database.
3. Go to system/config file. You need to set base url like `http://localhost/spice/` or what you might feet.
4. Now set the database host. Usually it’s `localhost`, database name is `spice` or the name you have created, the user is the database user name and set the password.

Your done!

GO TO the browser and then visit your site.

Let me know if you have any confusion, comments or feedback. I will really appreciate that to improve the framework. If anyone wants to contribute please click here.

Read this book Learning PHP, MySQL, JavaScript, CSS & HTML5: A Step-by-Step Guide to Creating Dynamic Websites to learn more on PHP.

Enjoy 🙂

Advertisements

Drupal 7: How to get user’s IP or hostname ?

In Drupal 7 how to find out any guest user or registered user’s IP address or hostname?

The answer is we can use session_id() which is stored in the ‘session’ table in the D7. It can return current user’s IP address or hostname by using session-id whether user is a guest or registered member.
So I write a function you can find it here.

https://github.com/tanveer-noman/neeocis-wp-support/tree/master/drupal-how-to-get-hostname

[OpenCart] how to install vQmod in OpenCart ?

opencart

Today, I came up here to make some easy pictures to install “vQmod” (Create xml patches for php files without editing the core system source code so called Virtual file modification system) in OpenCart 1.5.5.1

If you don’t know what is vQmod? then please read before we start.

vQmod: Virtual file modification system

Assume, we have installed a fresh copy of OpenCart 1.5.5.1 in the system

In this tutorial there are more than one way that vQmod can be installed in the opencart system.

Install vQmod in OpenCart manually:

Step-1: For self securitey! Backup or make a duplicate of your existing installed OpenCart files and folders in safe place.

Step-2: Download the latest version of vQMod

Step-3: Extract vqmod-x.x.x-opencart.zip and upload the vqmod folder to root of OpenCart directory.

Step-4: Now, we need to modify two files. One is /index.php and another one is /admin/index.php file

Search,

//File: /index.php 
// Startup
require_once(DIR_SYSTEM . 'startup.php');

// Application Classes
require_once(DIR_SYSTEM . 'library/customer.php');
require_once(DIR_SYSTEM . 'library/currency.php');
require_once(DIR_SYSTEM . 'library/tax.php');
require_once(DIR_SYSTEM . 'library/weight.php');
require_once(DIR_SYSTEM . 'library/length.php');
require_once(DIR_SYSTEM . 'library/cart.php');
require_once(DIR_SYSTEM . 'library/affiliate.php');

Replace with,

// vQmod
require_once('./vqmod/vqmod.php');
VQMod::bootup();

// VQMODDED Startup
require_once(VQMod::modCheck(DIR_SYSTEM . 'startup.php'));

// Application Classes
require_once(VQMod::modCheck(DIR_SYSTEM . 'library/customer.php'));
require_once(VQMod::modCheck(DIR_SYSTEM . 'library/currency.php'));
require_once(VQMod::modCheck(DIR_SYSTEM . 'library/tax.php'));
require_once(VQMod::modCheck(DIR_SYSTEM . 'library/weight.php'));
require_once(VQMod::modCheck(DIR_SYSTEM . 'library/length.php'));
require_once(VQMod::modCheck(DIR_SYSTEM . 'library/cart.php'));
require_once(VQMod::modCheck(DIR_SYSTEM . 'library/affiliate.php'));

and,

Search,

//File: /admin/index.php
// Startup
require_once(DIR_SYSTEM . 'startup.php');

// Application Classes
require_once(DIR_SYSTEM . 'library/currency.php');
require_once(DIR_SYSTEM . 'library/user.php'));
require_once(DIR_SYSTEM . 'library/weight.php');
require_once(DIR_SYSTEM . 'library/length.php');

Replace with,

// vQmod
require_once('../vqmod/vqmod.php');
VQMod::bootup();

// VQMODDED Startup
require_once(VQMod::modCheck(DIR_SYSTEM . 'startup.php'));

// Application Classes
require_once(VQMod::modCheck(DIR_SYSTEM . 'library/currency.php'));
require_once(VQMod::modCheck(DIR_SYSTEM . 'library/user.php'));
require_once(VQMod::modCheck(DIR_SYSTEM . 'library/weight.php'));
require_once(VQMod::modCheck(DIR_SYSTEM . 'library/length.php'));

Step-5: vQmod is now ready to use. Upload desired xml script to ‘vqmod/xml’ folder

Step-6: How this .xml file should look? For exampale we need to add a menu item “Homenet” under “Extension” menu in the admin panel. So we need to create a file named admin_menu_modification.xml in ‘vqmod/xml’ folder like;

‘vqmod/xml/admin_menu_modification.xml’

<!--
#author: Tanveer Noman
#file name: admin_menu_modification.xml
#description: This file is demonstrating how to write a simple xml file to add new
menu item named "Homenet" in admin menu section
-->
<modification>
	<id>Admin Menu Modification</id>
	<version>1.5.x</version>
	<vqmver>2.2.1</vqmver>
	<author>tanveer.noman@gmail.com</author>
	<file name="admin/controller/common/header.php">
		<operation>
			<search position="after"><![CDATA[$this->data['text_feed'] = $this->language->get('text_feed');]]></search>
			<add><![CDATA[$this->data['text_homenet'] = $this->language->get('text_homenet');]]></add>
		</operation>
		<operation>
			<search position="after"><![CDATA[$this->data['feed'] = $this->url->link('extension/feed', 'token=' . $this->session->data['token'], 'SSL');]]></search>
			<add><![CDATA[$this->data['homenet'] = $this->url->link('homenet/homenet', 'token=' . $this->session->data['token'], 'SSL');]]></add>
		</operation>
	</file>
	<file name="admin/view/template/common/header.tpl">
		<operation>
			<search position="after"><![CDATA[<li><a href="<?php echo $feed; ?>"><?php echo $text_feed; ?></a></li>]]></search>
			<add><![CDATA[<li><a href="<?php echo $homenet; ?>"><?php echo $text_homenet; ?></a></li>]]></add>
		</operation>		
	</file>
	<file name="admin/language/english/common/header.php">
		<operation>
			<search position="after"><![CDATA[$_['text_feed']                        = 'Product Feeds';]]></search>
			<add><![CDATA[$_['text_homenet']                     = 'Homenet';]]></add>
		</operation>		
	</file>
</modification>

Wallah! You are done!

Step-7: Now log in to admin panel. Mouse over to “Extension” menu item, if everything works good then you can see a new menu item “Homenet” is added whithout changing any core system files.

homenet

Install vQmod in OpenCart using Autoinstaller:

Step-1: Download the latest version of vQMod and extract it.
Step-2: Use your FTP to upload the folder “vqmod” to your opencart store. Make sure vqmod/vqcache folders are writable (either 755 or 777).
Step-3: Now browse http://www.your-domain.com/vqmod/install
Step-4: If everything goes well, you should get a success message. If not then check permissions.

***Troubleshooting***
– Make sure you are giving the currect vqmod path like; require_once(‘../vqmod/vqmod.php’);
– Check if the vqmod folder and the vqmod/vqcache folders are writable (755)
– Verify that there are new “vq2” files in the “vqmod/vqcache” folder after loading your store homepage
– Some times you may need to create “mods.cache” file inside “vqmod” folder and make it writable (755)
– You may need to create logs” folder inside “vqmod” folder and make it writable (755)

For more information:

http://docs.opencart.com/display/opencart/OpenCart+1.5+Home
http://www.opencart.com/index.php?route=extension/extension
http://forum.opencart.com
https://github.com/opencart/opencart/issues
http://opencart.hostjars.com
http://opencart-help.com

Related Links:
[Video] http://code.google.com/p/vqmod/wiki/Install_OpenCart

That’s all!

You can inbox me or drop your comments/feedbak here I will reply on that regards.

Thanks for reading and don’t forget to share!

[PHP] How to get longest string from array ?

Hope it will save your time. Sometimes in PHP we may need to find the longest string from an array. So you can get this solutions in google but I made a function to make it more usable. It will return maximum lenght, key and the string form that array.

Clieck Here to see the code

Don’t forget to share!.

Cheers!

[phpMyAdmin] How to enable Designer in phpMyAdmin

Some times we need to create ERD. In phpMyAdmin this can be done very easily. Just you need to change some configuration.

I am assuming that you have already installed phpMyAdmin (in my case it’s 3.4.3.2 version). You have to follow these simple steps;

Step 1: Open the file

libraries/config.default.php

Step 2: Find and change these configuration like this:

$cfg[‘Servers’][$i][‘pmadb’] = ‘phpmyadmin’;
$cfg[‘Servers’][$i][‘bookmarktable’] = ‘pma_bookmark’;
$cfg[‘Servers’][$i][‘relation’] = ‘pma_relation’;
$cfg[‘Servers’][$i][‘table_info’] = ‘pma_table_info’;
$cfg[‘Servers’][$i][‘table_coords’] = ‘pma_table_coords’;
$cfg[‘Servers’][$i][‘pdf_pages’] = ‘pma_pdf_pages’;
$cfg[‘Servers’][$i][‘column_info’] = ‘pma_column_info’;
$cfg[‘Servers’][$i][‘history’] = ‘pma_history’;
$cfg[‘Servers’][$i][‘designer_coords’] = ‘pma_designer_coords’;

Step 3: Now, create a database name phpmyadmin. You can copy and paste in these script.

Click to download phpmyadmin.sql

**NOTE: People are getting some unknown characters like ‘–’ and facing difficulties to load the SQL into the database. So I remove the SQL block form here and upload that in my github repository. Please download from there and accept my apologies for that.

you are done!

Step 4: Now select any of your database from the left side database list

Step 5: You will get a “Designer” tab

Step 6: Now, you need to logged out to phpmyadmin and then log-in again to be able to see the Designer tab. Click on the “Designer” tab and you will get something like this:

That’s all, you can now enjoy the model 😉

Updated suggest by:
– Meri Alvarado

Create your own PHP framework using MVC design pattern (Part – 1)

Why Model-View-Controller ?

I am really thinking about Model-View-Controller (formally known as MVC) design pattern for PHP. So read these articles and find some really interesting concepts and make some understanding what we need to think and or not to when we make our own PHP framework.

You may read these articles too if you need have some clear understanding for go further.

1. http://www.tonymarston.net/php-mysql/model-view-controller.html
2. http://phpro.org/tutorials/Model-View-Controller-MVC.html
3. http://www.devshed.com/c/a/PHP/An-Introduction-to-Simulating-the-Model-View-Controller-Schema-in-PHP/

However, my point’s to re-factoring my PHP frameworks are as follows

1. All access of the site must be controlled with index.php.
2. A single point access of all entry is maintained by .htaccess file.
3. Hiding the index.php file form URL.
4. Creating SEO and user friendly URL’s.

cont.

Create your own PHP framework using MVC design pattern (Part – 2)

%d bloggers like this: