This is the project of DSMWare course of group formed by LUONG Trong Hieu & NGUYEN Duc Trung.
SET UP ENVIRONMENT FOR DEVELOPMENT
1. You can use some php IDEs such as Eclipse, phpStorm (recommended) to develop
- If you use phpStorm, the configuration file puted in responsitory might be useful.
- Install xdebug for debugging
2. Install php5 & Apache server by following this instruction (for ubuntu users) and this instruction for general users.
- You should choose another directory for storing root directory of server (default is /var/www) because the other IDEs difficultly wirte into these directory. You can choose /home/www/.
3. Install phpmyAdmin by following this instruction or this (for ubuntu users).
After installation, you can navigate your browser to http://localhost/phpmyadmin to work on
4. Install Git (if not any)
PROJECT STRUCTURE
The project is based on MVC Model: (written by following this tutorial and this , but who care ?)
The directories structure:
├── Config : all configuration of website
│ └── Modules : contain config file of each module
│ └── Amazon
├── Controller : all controllers
│ └── Amazon : controllers of module 'Amazon'
├── Core : all core classes
├── design : all stuff related to 'how do users look'
│ ├── css
│ ├── images
│ └── js
├── Lib : all libraries
│ ├── Amazon : library from Amazon
│ └── Drivers : class to connect and access mySQL data
├── samples : sample codes to test libraries (often download from tutorials)
└── View : all view html templates (with some php variables) for filling up data
└── Amazon : templates of module amazon
THE URL PROCESSING
The first, let see the structure of URL when user request. The well-form URL is:
http://example.com/index.php/controller/action?param1=value1¶m2=value2
So, with url http://localhost, when passed to our application, we will check and fill up to: http://localhost/index.php/index/index (if controller or action is missing)
What is index.php ?
Well, index.php is the only one entrance of the application. All request will pass to this file. Therefore, we only need to load configurations & some pre-processing here. It will make our application easier to maintain.
What is the first /index ?
This is the name of controller will handle the request. Our application will capitalize the first letter, and add prefix 'Controller_' to forming 'Controller_Index'. It's the name of controller class will process request.
What is the second /index ?
This is the name of action will be done.
After create a new instance of class Controller_Index, we will call function 'indexFunction'.
OK, for another example, the url: localhost/index.php/site/search?s=amazon&sq=alice will be broken, and splitted to call function 'searchAction' and pass parameters $params['s']='amazon' and $params['sq']='alice' ($params is the name of input parameters of this function)
AUTOLOAD CLASS
Whenever you call $x = new Class_X(); but the php file contains class X is never included, php will consider to auto include and load this file.
But, how can php know where this file locates ?
We can guild for php by function 'autoload' in Core/Core.php.
Every unknown class name will be broken into directory form. For example, class 'Controller_Amazon_home' will located at Controller/Amazon/Home.php
This function is used very often in many framwork such as Magento, Wordpress, Zend...
HOW DOES IT WORK ?
Class 'Controller' and all class inherit it have a field named 'view' (an instance of Core_View class) and this field will render the interface.
When the instance of controller is created, it will create new view.
When we call function , let say, indexAction, every Model instances (which will get data from database) will be declared here and launch them.
After that, the view will be initialized and filled up with data.
$this->view->title_url = "Homepage";
$this->view->header = (new Core_View('header'))->render(FALSE);
$this->view->footer = (new Core_View('footer'))->render(FALSE);
// set template is home.php and render
$this->view->setTemplate('home');
$this->view->render();
The interesting thing is title_url, header... is never declared in view class ! when $this->view->title_url = "Homepage";
is involked, the view will assgin $data['title_url']="Homepage" via function:
public function __set($index, $value){
$this->data[$index] = $value;
}
we overrided the default function of php for class Core_View.
So, write any field you want: $this->view->name, $this->view->XXX...
and it will be pass into $data['name'], $data['XXX']... and be used in tempated file (you can see in View/home.php)
$this->view->header = (new Core_View('header'))->render(FALSE); will create new Core_View instance with template is 'header.php' in View directory and then call function 'render' of this function. Parameter FALSE means: don't output into screen, keep it and return value for whom called it.
In render function, the template will be load (by call 'include'), all variables will be replaced by its value.
SEARCHING MODULE
All searching module will have its MVC model. The controller will inherit from Controller_SearchingAbstract, implements 'indexFunction' and 'searchingFunction'.
When localhost/index.php/site/search?s=amazon&sq=alice is requested, the application will call 'searchingFunction' of Controller_Site class (Controller/Site.php). In this function, we will check if parameter 's' exists or not. If yes, create new instance Controller of amazon module. But, how can we know what controller class of this module ? Oh, it's declared at Config/Amazon/Amazon.xml and loaded automacally at the begging of application (in Index.php)
<amazon>
<active>true</active>
<class>Controller_Amazon_Home</class>
</amazon>
OK, so the searchingFunction of Controller_Amazon_Home will be called. In this function, we will use web services of amazon to get data. That's all.
So, whenever well need to add new searching website, write it by MVC, and copy into application, create new config file. It's ok. We don't need to modify any existing code.
