Developpez.com - Rubrique PHP

Le Club des Développeurs et IT Pro

Apprendre à réaliser votre blog avec le framework CodeIgniter 3

Un tutoriel de Sébastien Adam

Le 2018-05-30 13:14:11, par Community Management, Community Manager
Chers membres du club,

J'ai le plaisir de vous présenter ce tutoriel de Sébastien Adam pour vous apprendre à créer un blog avec le framework CodeIgniter.

Bienvenue sur ce tutoriel « Réalisez votre blog avec le framework CodeIgniter 3 ».
Cela fait déjà quelques années maintenant que je développe des sites web avec différents langages (Perl, PHP, C#…) et en utilisant différents outils (des gestionnaires de contenu, des frameworks…).

Lorsque j'ai découvert le framework PHP CodeIgniter, j'ai été séduit, et surpris, par sa simplicité et sa facilité d'apprentissage. Là où d'autres frameworks se comportent parfois comme de vraies « usines à gaz », CodeIgniter va à l'essentiel. Et j'espère, grâce à ce tutoriel, vous faire partager mon enthousiasme.
Bonne lecture

Retrouvez les meilleurs cours et tutoriels pour apprendre PHP
  Discussion forum
6 commentaires
  • pascal-od
    Membre habitué
    Bonjour,

    Petite remarque sur la suppression des fichiers index.html devenus inutiles suite à la modification de l'arborescence. Avec git les répertoires vides ne sont pas pris en compte, on risque donc, si on déploie l'application avec git sur un serveur, de perdre des répertoires (devenus vides en supprimant les fichiers index.html) nécessaires au fonctionnement de l'application, je pense notamment aux répertoires logs et cache.
  • demenvil
    Membre averti
    Envoyé par pascal-od
    Bonjour,

    Petite remarque sur la suppression des fichiers index.html devenus inutiles suite à la modification de l'arborescence. Avec git les répertoires vides ne sont pas pris en compte, on risque donc, si on déploie l'application avec git sur un serveur, de perdre des répertoires (devenus vides en supprimant les fichiers index.html) nécessaires au fonctionnement de l'application, je pense notamment aux répertoires logs et cache.
    Oui par défaut il vaut mieux les laisser en place.
    Après c'est le htaccess de l'apps qui gère la suite.
    Puis ce n'est pas cela qui va alourdir un projet.

    Après peut-être qu'une modification de la conf de git s'impose, personnellement je n'ai jamais eu ce genres de problèmes
  • demenvil
    Membre averti
    Envoyé par yunisu
    No direct script access allowed quand il y a
    $this->load->library('form_validation');
    dans le controleur
    Aide-moi s'il vous plait
    Bonjour,

    Il serait pas mal de nous faire passer plus de code et d'explications si tu veux résoudre ton problème.
  • yunisu
    Nouveau membre du Club
    Envoyé par demenvil
    Bonjour,

    Il serait pas mal de nous faire passer plus de code et d'explications si tu veux résoudre ton problème.
    Quand je mis le fichier .htaccess le site ne marche pas
  • yunisu
    Nouveau membre du Club
    Bonjour,
    Désolé pour le retard
    Mon dossier est dans le wamp/www/blogEtienner.fr/blogpdf
    Mon application dans le wamp/www/blogEtienner.fr/application
    Mon system dans le wamp/www/framework/codeigniter3

    Dans mon application/config/config.php:

    Code php :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
      
    <?php 
    defined('BASEPATH') OR exit('No direct script access allowed'); 
      
    $config['base_url'] = 'http://localhost/blogetienner.fr/blogpdf/'; 
      
    $config['index_page'] = ''; 
      
    $config['uri_protocol']	= 'REQUEST_URI'; 
      
    $config['url_suffix'] = '.html'; 
      
    $config['language']	= 'english'; 
      
    $config['charset'] = 'UTF-8'; 
      
    $config['enable_hooks'] = FALSE; 
      
    $config['subclass_prefix'] = 'MY_'; 
      
    $config['composer_autoload'] = FALSE; 
      
    $config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-'; 
      
    $config['allow_get_array'] = TRUE; 
    $config['enable_query_strings'] = FALSE; 
    $config['controller_trigger'] = 'c'; 
    $config['function_trigger'] = 'm'; 
    $config['directory_trigger'] = 'd'; 
      
    $config['log_threshold'] = 0; 
      
    $config['log_path'] = ''; 
      
    $config['log_file_extension'] = ''; 
      
    $config['log_file_permissions'] = 0644; 
      
    $config['log_date_format'] = 'Y-m-d H:i:s'; 
      
    $config['error_views_path'] = ''; 
      
    $config['cache_path'] = ''; 
      
    $config['cache_query_string'] = FALSE; 
      
    $config['encryption_key'] = ''; 
      
    $config['sess_driver'] = 'files'; 
    $config['sess_cookie_name'] = 'ci_session'; 
    $config['sess_expiration'] = 7200; 
    $config['sess_save_path'] = NULL; 
    $config['sess_match_ip'] = FALSE; 
    $config['sess_time_to_update'] = 300; 
    $config['sess_regenerate_destroy'] = FALSE; 
      
    $config['cookie_prefix']	= ''; 
    $config['cookie_domain']	= ''; 
    $config['cookie_path']		= '/'; 
    $config['cookie_secure']	= FALSE; 
    $config['cookie_httponly'] 	= FALSE; 
      
    $config['standardize_newlines'] = FALSE; 
      
    $config['global_xss_filtering'] = FALSE; 
      
    $config['csrf_protection'] = FALSE; 
    $config['csrf_token_name'] = 'csrf_test_name'; 
    $config['csrf_cookie_name'] = 'csrf_cookie_name'; 
    $config['csrf_expire'] = 7200; 
    $config['csrf_regenerate'] = TRUE; 
    $config['csrf_exclude_uris'] = array(); 
      
    $config['compress_output'] = FALSE; 
      
    $config['time_reference'] = 'local'; 
      
    $config['rewrite_short_tags'] = FALSE; 
      
    $config['proxy_ips'] = '';

    Dans mon blogpdf/.gitignore

    Code php :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
      
    .DS_Store 
      
    /application/cache/ 
    /application/logs/ 
      
    /www/user_guide/ 
    /framework/ 
      
    # IDE Files 
    #------------------------- 
    /nbproject/ 
    .idea/* 
      
    ## Sublime Text cache files 
    *.tmlanguage.cache 
    *.tmPreferences.cache 
    *.stTheme.cache 
    *.sublime-workspace 
    *.sublime-project

    Dans mon blogpdf/index.php
    Code php :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
      
    <?php 
    /** 
     * CodeIgniter 
     * 
     * An open source application development framework for PHP 
     * 
     * This content is released under the MIT License (MIT) 
     * 
     * Copyright (c) 2014 - 2016, British Columbia Institute of Technology 
     * 
     * Permission is hereby granted, free of charge, to any person obtaining a copy 
     * of this software and associated documentation files (the "Software"), to deal 
     * in the Software without restriction, including without limitation the rights 
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
     * copies of the Software, and to permit persons to whom the Software is 
     * furnished to do so, subject to the following conditions: 
     * 
     * The above copyright notice and this permission notice shall be included in 
     * all copies or substantial portions of the Software. 
     * 
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
     * THE SOFTWARE. 
     * 
     * @package	CodeIgniter 
     * @author	EllisLab Dev Team 
     * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) 
     * @copyright	Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/) 
     * @license	http://opensource.org/licenses/MIT	MIT License 
     * @link	https://codeigniter.com 
     * @since	Version 1.0.0 
     * @filesource 
     */ 
      
    	define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development'); 
      
    switch (ENVIRONMENT) 
    { 
    	case 'development': 
    		error_reporting(-1); 
    		ini_set('display_errors', 1); 
    	break; 
      
    	case 'testing': 
    	case 'production': 
    		ini_set('display_errors', 0); 
    		if (version_compare(PHP_VERSION, '5.3', '>=')) 
    		{ 
    			error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED); 
    		} 
    		else 
    		{ 
    			error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE); 
    		} 
    	break; 
      
    	default: 
    		header('HTTP/1.1 503 Service Unavailable.', TRUE, 503); 
    		echo 'The application environment is not set correctly.'; 
    		exit(1); // EXIT_ERROR 
    } 
      
    	$system_path = '../framework/codeigniter3'; 
      
    	$application_folder = 'application'; 
      
    	$view_folder = ''; 
      
      
    	if (defined('STDIN')) 
    	{ 
    		chdir(dirname(__FILE__)); 
    	} 
      
    	if (($_temp = realpath($system_path)) !== FALSE) 
    	{ 
    		$system_path = $_temp.DIRECTORY_SEPARATOR; 
    	} 
    	else 
    	{ 
    		// Ensure there's a trailing slash 
    		$system_path = strtr( 
    			rtrim($system_path, '/\\'), 
    			'/\\', 
    			DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR 
    		).DIRECTORY_SEPARATOR; 
    	} 
      
    	// Is the system path correct? 
    	if ( ! is_dir($system_path)) 
    	{ 
    		header('HTTP/1.1 503 Service Unavailable.', TRUE, 503); 
    		echo 'Your system folder path does not appear to be set correctly. Please open the following file and correct this: '.pathinfo(__FILE__, PATHINFO_BASENAME); 
    		exit(3); // EXIT_CONFIG 
    	} 
      
    	define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME)); 
      
    	// Path to the system directory 
    	define('BASEPATH', $system_path); 
      
    	// Path to the front controller (this file) directory 
    	define('FCPATH', dirname(__FILE__).DIRECTORY_SEPARATOR); 
      
    	// Name of the "system" directory 
    	define('SYSDIR', basename(BASEPATH)); 
      
    	// The path to the "application" directory 
    	if (is_dir($application_folder)) 
    	{ 
    		if (($_temp = realpath($application_folder)) !== FALSE) 
    		{ 
    			$application_folder = $_temp; 
    		} 
    		else 
    		{ 
    			$application_folder = strtr( 
    				rtrim($application_folder, '/\\'), 
    				'/\\', 
    				DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR 
    			); 
    		} 
    	} 
    	elseif (is_dir(BASEPATH.$application_folder.DIRECTORY_SEPARATOR)) 
    	{ 
    		$application_folder = BASEPATH.strtr( 
    			trim($application_folder, '/\\'), 
    			'/\\', 
    			DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR 
    		); 
    	} 
    	else 
    	{ 
    		header('HTTP/1.1 503 Service Unavailable.', TRUE, 503); 
    		echo 'Your application folder path does not appear to be set correctly. Please open the following file and correct this: '.SELF; 
    		exit(3); // EXIT_CONFIG 
    	} 
      
    	define('APPPATH', $application_folder.DIRECTORY_SEPARATOR); 
      
    	// The path to the "views" directory 
    	if ( ! isset($view_folder[0]) && is_dir(APPPATH.'views'.DIRECTORY_SEPARATOR)) 
    	{ 
    		$view_folder = APPPATH.'views'; 
    	} 
    	elseif (is_dir($view_folder)) 
    	{ 
    		if (($_temp = realpath($view_folder)) !== FALSE) 
    		{ 
    			$view_folder = $_temp; 
    		} 
    		else 
    		{ 
    			$view_folder = strtr( 
    				rtrim($view_folder, '/\\'), 
    				'/\\', 
    				DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR 
    			); 
    		} 
    	} 
    	elseif (is_dir(APPPATH.$view_folder.DIRECTORY_SEPARATOR)) 
    	{ 
    		$view_folder = APPPATH.strtr( 
    			trim($view_folder, '/\\'), 
    			'/\\', 
    			DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR 
    		); 
    	} 
    	else 
    	{ 
    		header('HTTP/1.1 503 Service Unavailable.', TRUE, 503); 
    		echo 'Your view folder path does not appear to be set correctly. Please open the following file and correct this: '.SELF; 
    		exit(3); // EXIT_CONFIG 
    	} 
      
    	define('VIEWPATH', $view_folder.DIRECTORY_SEPARATOR); 
      
    require_once BASEPATH.'core/CodeIgniter.php';


    Dans application/config/routes.php
    Code php :
    1
    2
    3
    4
    5
    6
    7
    8
    9
      
    <?php 
    defined('BASEPATH') OR exit('No direct script access allowed'); 
      
    $route['default_controller'] = 'site'; 
    $route['(:any)'] = 'site/$1'; 
      
    $route['404_override'] = ''; 
    $route['translate_uri_dashes'] = FALSE;

    Dans application/controllers/Site.php

    Code php :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
      
      
    <?php 
    defined('BASEPATH') OR exit('No direct script access allowed'); 
      
    /** 
    *  
    */ 
    class Site extends CI_Controller 
    { 
      
    	public function index() 
    	{ 
    		$data["title"] = "Page d'accueil"; 
      
    		$this->load->view('common/header', $data); 
    		$this->load->view('site/index', $data); 
    		$this->load->view('common/footer', $data); 
    	} 
    }

    Dans application/views/common/header.php

    Code php :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
      
    <!DOCTYPE html> 
    <html> 
    	<head> 
    		<meta charset="UTF-8"> 
    		<meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    		<meta name="viewport" content="width=device-width, initial-scale=1"> 
    		<title><?= $title ?></title> 
    		<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css"> 
    		<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap-theme.min.css"> 
    	</head> 
    	<body> 
    		<nav class="navbar navbar-inverse"> 
    			<div class="container-fluid"> 
    				<div class="navbar-header"> 
    					<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" 
    					data-target="#main_nav" aria-expanded="false"> 
    						<span class="sr-only">Toggle navigation</span> 
    						<span class="icon-bar"></span> 
    						<span class="icon-bar"></span> 
    						<span class="icon-bar"></span> 
    					</button> 
    					<a href="navbar-brand" href="/index.html">Sebastien Adam</a> 
    				</div> 
    				<div class="collapse navbar-collapse" id="main_nav"> 
    					<ul class="nav navbar-nav"> 
    						<li><a href="/index.html"> Accueil</a></li> 
    					</ul> 
    				</div> 
    			</div> 
    		</nav>

    Dans application/views/site/index.php

    Code php :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
      
    <div class="jumbotron"> 
    	<div class="container"> 
    		<h1> <?= $title ?></h1> 
    	</div> 
    </div> 
    <div class="container"> 
    	<p>Bla, bla, bla...</p> 
    	<p>Bla, bla, bla...</p> 
    	<p>Bla, bla, bla...</p> 
    </div>

    Dans application/views/common/footer.php

    Code php :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
      
    		<div class="container"> 
    			<hr> 
    			<footer> 
    				<p>© Sebastien Adam 2016</p> 
    			</footer> 
    		</div> 
    		<script src="bootstrap/js/jquery-2.1.4.min.js"></script> 
    		<script src="bootstrap/js/bootstrap.min.js"></script> 
    	</body> 
    </html>

    Le problème est quand je clique sur accueil et Sébastien Adam, il dit Not Found. Aide moi s'il vous plait
  • yunisu
    Nouveau membre du Club
    No direct script access allowed quand il y a
    $this->load->library('form_validation');
    dans le controleur
    Aide-moi s'il vous plait