Basic file/working structure of Codeigniter PHP Framework

In my previous articles, I have explained about some programming concepts in CodeIgniter. But, some of the friends asking about the Basic working structure and file structure of CI. So, In this article, we are going to discussing about the Basic working structure of CodeIgniter.

Codeigniter is a fully features PHP Framework which facilitates the most of the web development features. Codeigniter is mainly developed for the PHP developers who need a full featured PHP web applications. Design integration in Codeigniter is much easier than other PHP Frameworks.

In my previous article, We have discussed about the "Installing and Configuring Codeigniter framework on localhost". In this article, We will discuss about the Basic workflow and File Structure of Codeigniter.

Codeigniter folder structure :

"system" is the main CodeIgniter folder under the installation. It contains the core Codeigniter code files. It also contains no.of sub folders. One of the main sub folder is "application". Remember "application" folder which is main development hostage for you.

"application" folder contains your HTML files, your data base configuration file and your whole control structure of Codeigniter. The rest of the system folder contains CodeIgniter core code, libraries, helpers and other files.

The "application" folder has more sub-folders inside which are responsible for keeping code files separate. Such as models, controllers, config and views.

The "application" folder contains some important files to run Codeigniter. Those files are given below with details.

1, config.php - which holds your localhost path to your Codeigniter project. It also contains some predefined rules such as permitted URLs, session expiration time, hooks enabling and some others.

2, database.php - which contains the database configurations. you need to give host name password and user name for connecting to db.

Next folder is controller which holds all of your controllers. Controller is basic part of Codeigniter.

Controller is a php class file and inherits from a base Codeigniter Controller class using the extends keyword.

Codeigniter controller names should start with an uppercase letter. Inside the constructor, the parent::Controller() statement is needed to make sure that the current class inherits functionality of the Controller class. Developers can call functions internal to a controller using $this->function() but codeigniter controllers cannot call functions inside another controller.

Create a Web Applications with Symfony2 Framework Part 2 - Bundles and routing

In my previous article, I have explained about How to create a web application with Symfony 2 framework part 1. In this article, I'm going to explain about, How to bundle and routing with Symfony2 Framework.

If you have any doubt in installing and configuring the Symfony2 framework, Check my previous article "Installing and Configuring Symfony2 framework on Linux Ubuntu OS". If you need more details about, How to create a web application with Symfony2 framework, Check the article "Create a Web application with Symfony2 Framework - Part 1".


Step 1 : Create Bundle

At the beginning, Symfony 2 Framework makes sense that the main bundle "TestSymfonyBundle" create. Go to the cmd.exe the path to the root directory of the web application with php app/console it calls help. There you can read about the various basic commands and become better acquainted with the Symfony application consoles familiar.


With the following command we can create our example project finally the bundle : phpapp/console init: bundle Test\SymfonyBundle src/

* init: bundle is the command to initialize the bundle
* Test \ SymfonyBundle is the PHP 5.3 namespace
* src / corresponds to the folder in which the bundle is to be created

If everything goes well, an application of the consoles Symfony2 Framework already suggests the next steps. Additionally, you can look in the editor's choice created DefaultControllerFactory under / src / Test / SymfonyBundle / controller.



Step 2 : Bundle activate and integrate into the application

Symfony2 Framework is very economical in "guessing" of components or plugins, as this behavior costs a lot of performance and programmer unnecessarily squeezed into a programming style. Therefore, the developer must active the bundle tell the autoloader.

For this we open the first AppKernel (/app/AppKernel.php) and adds the newly created SymfonyBundle in the Function register bundles () added.
To do this simply extends the variable $bundles by the following lines:

//Example Scandio
new  Test\SymfonyBundle\TestSymfonyBundle (),

We also can use the namespace makes sense, we have to register it in the autoloader. Nothing easier than that:

/// App/autoload.php
$Loader -> register namespaces(array(
    ...
    / / Symfony
    'Symfony' => __ DIR__. '/.. / src'
));

For our example, these settings are sufficient. As can be seen in the variety of configurations allow the developer Symfony2 very much freedom to design their applications as they wish.


Step 3 : Configuring Routing

An important component of Symfony2 is the routing. Routing is finding the way here the URL to the appropriate controller action. Unlike, for example CakePHP routes here are not automatically created (these are mostly based on conventions), but the developer the way is treated as a route before.

This turns - with the necessary programming discipline - to be very advantageous. As a developer, you can customize the URLs here with his wishes, without the risk of violating coding standards, and so produce unnecessary mistakes.

As already described in the first part of this series, we need for our application six basic routes ( index, add, edit, delete, markAsDone, markAsUndone ). The Referenzauf the routes are stored in the /app/config/routing.yml file. If that is not common or YML format to another format (ex.XML) is preferred, this can also use in Symfony2 Framework. The framework allows the programmer a lot of liberties here, as long as it is configured.

Based on the sample routes you can already see the Best Practices: The relevant route information should be stored in the bundle, in order to facilitate a transfer later. The following must be entered in the routing.yml so that the routes are detected by the application: #/app/config/routing.yml # Test Symfony2 tutorial test_symfony_tutorial: resource: "@TestSymfonyBundle/Resources/config/routing.yml"

Next the routes are entered into the currently referenced routing config. For this, I recommend reading the excellent again declared Symfony2 online manuals . There is explained clearly and in detail, what routes are and how they are created.

Maybe it will surprise some readers that, add/edit action in two different controller functions have been split. The point is that through this code is readable and understandable better. Additionally, one can wiedervewenden so features or actions directly. #index: Show all tasks ordered by priority test_symfony_index: pattern:/symfony defaults: {_controller: TestSymfonyBundle:Task:index}

# View: Display a task 
test_symfony_view: 
pattern: /symfony/view/{taskId} 
defaults: {_controller: TestSymfonyBundle: Task: view} 
requirements: 
taskId: \ d +

# Add: ShowForm 
test_symfony_add_prepare: 
pattern: /symfony/add 
defaults: {_controller: TestSymfonyBundle:Task:addPrepare} 
requirements: 
_method: get

# Add: Process Form Data to add new task 
: test_symfony_add_prepare 
pattern: /symfony/add 
defaults: {_controller: TestSymfonyBundle:Task:addProcess} 
requirements: 
_method: post

# Edit: Show form with prevoius task data 
test_symfony_edit_prepare: 
pattern: /symfony/edit/{taskId} 
defaults: {_controller: TestSymfonyBundle:Task:editPrepare} 
requirements: 
_method: get 
taskId: \ d +

# Edit: Process Form Data to edit task 
: test_symfony_edit_prepare 
pattern: / symfony / edit / {taskId} 
defaults: {_controller: TestSymfonyBundle:Task:editProcess} 
requirements: 
_method: post 
taskId: \ d +

# Delete: deletes an existing task 
test_symfony_delete: 
pattern: /symfony/delete/{taskId} 
defaults: {_controller: TestSymfonyBundle:Task:delete} 
requirements: 
taskId: \ d +

# MarkAsDone: Mark an existing task as done 
test_symfony_markAsDone: 
pattern: /symfony/done/{taskId} 
defaults: {_controller: TestSymfonyBundle:Task:markAsDone} 
requirements: 
taskId: \ d +

# MarkAsDone: Mark an existing task as undone 
test_symfony_markAsUndone: 
pattern: /symfony/undone/{taskId} 
defaults: {_controller: TestSymfonyBundle: Task: markAsUndone} 
requirements: 
taskId: \ d +


If all routes were added successfully, we can test via console whether the required routes are recognized by Symfony2. The following output appears on successful route configuration:

C:\path\to\project> php app/console router: debug [router] Current routes Name Method Pattern _wdt ANY/_wdt/{token}_profiler_search ANY/_profiler/search _profiler_purge ANY/_profiler / purge _profiler_import ANY / _profiler / import ANY / _profiler / export / {token}. txt _profiler_export _profiler_search_results ANY / _profiler / {token} / search / results _profiler ANY / _profiler / {token} _configurator_home ANY / _configurator / _configurator_step ANY / _configurator / step / {index} _configurator_final ANY / _configurator / final _welcome ANY / _demo_login ANY / demo / secured / login _security_check ANY / demo / secured / login_check _demo_logout ANY / demo / secured / logout acme_demo_secured_hello ANY / demo / secured / hello _demo_secured_hello ANY / demo / secured / hello / {name} _demo_secured_hello_admin ANY / demo / secured / hello / admin / {name} _demo ANY / demo / _demo_hello ANY / demo / hello / {name} _demo_contact ANY / demo / contact test_symfony_index ANY / symfony test_symfony_view ANY / symfony / view / {taskId} test_symfony_add_prepare POST / symfony / add test_symfony_edit_prepare POST / symfony / edit / {taskId} test_symfony_delete ANY / symfony / delete / {taskId} test_symfony_markAsDone ANY / symfony / done / {taskId} test_symfony_markAsUndone ANY / symfony / undone / { taskId}

As you can see, our previously entered routes are available in the application. To those now filled with "life", we will create in the next part of the task controller that handles the processing and presentation.