[go: up one dir, main page]

0% found this document useful (0 votes)
135 views9 pages

Guide Informatique PDF

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

CodeIgniter 4

The local development server can be customized with three command line
options:
You can use the --host CLI option to specify a different host to run the
application at:

php spark serve --host=example.dev

By default, the server runs on port 8080 but you might have more than
one site running, or already have another application using that port.
You can use the --port CLI option to specify a different one:

php spark serve --port=8081

You can also specify a specific version of PHP to use, with the --php
CLI option, with its value set to the path of the PHP executable you
want to use:

$query = $db->table('table_name')->get();

foreach ($query->getResult() as $row)


{
echo $row->title;
}

Query Builder Insert

$data = [
'title' => $title,
'name' => $name,
'date' => $date
];

$db->table('mytable')->insert($data); // Produces: INSERT INTO m

public function selectCompanyStatus($columns = '*') {


return $this->db->table('status_list')
->select($columns)
->where('type', 'company')
->orderBy('name ASC')
->get()
->getResultArray();
}

$builder = $this->db->table('companies_to_company');
return $builder->selectCount($column, 'total')
->where($column, $company_id)
->get()
->getRowArray();

$query = $this->db->query('SELECT title FROM my_table');

foreach ($query->getResult() as $row)


{
echo $row->title;
}

$builder->db->table('blog');
$builder->select('*');
$builder->join('comments', 'comments.id = blogs.id');
$query = $builder->get();

$builder = $this->db->table('companies');
return $builder->select($columns)
->getWhere(['id' => $company_id])
->getRowArray();

public function selectCountry($id, $columns = '*') {


return $this->db->table('countries')
->select($columns)
->getWhere(['id' => $id])
->getRowArray();
}

$this->db->table('companies_billing')->update($data, ['id' => $billing_id])

return redirect()->route('forgot_password');

Notice that there are no braces. Instead, the end brace is replaced with
endforeach.
Each of the control structures listed above has a similar closing
syntax: endif, endfor, endforeach, and endwhile
Also notice that instead of using a semicolon after each structure (except the
last one),
there is a colon. This is important!
Here is another example, using if/elseif/else. Notice the colons:

<?php if ($username === 'sally') : ?>

<h3>Hi Sally</h3>

<?php elseif ($username === 'joe') : ?>

<h3>Hi Joe</h3>

<?php else : ?>

<h3>Hi unknown user</h3>

<?php endif ?>

SESSION CI4 :

To access and initialize the session:

$session = \Config\Services::session();

The $config parameter is optional - your application configuration. If not


provided, the services register will instantiate your default one.

Once loaded, the Sessions library object will be available using:


$session

$session->set($array);

Ex : $this->session->set($sessionData);

$newdata = [
'username' => 'johndoe',
'email' => 'johndoe@some-site.com',
'logged_in' => TRUE
];

$this->session->set($newdata);

Get session en CodeIgniter :


Récuper session user_id :
$this->session->get('user_id')

$this->session->setFlashdata('item', 'value');
$this->session->getFlashdata('item');

Supprimer une session :


$this->session->remove([‘connect_id’, 'id', 'user_id']);
$this->session->remove('id') ;

Note :

The getFlashdata() method returns NULL if the item cannot be found.

// Go back to the previous page


return redirect()->back();

// Go to specific UI
return redirect()->to('/admin');
// Go to a named/reverse-routed URI
return redirect()->route('named_route');

// Keep the old input values upon redirect so they can be used by the `old()` return
redirect()->back()->withInput();

// Create the route


$route->add('auth/login', 'Users::login', ['as' => 'login']);

// Use it in a view
<a href="<?= route_to('login') ?>">Login</a>

// The route is defined as:


$routes->add('users/(:id)/gallery(:any)', 'Galleries::showUserGallery/$1/$2', ['as
' => 'user_gallery');

// Generate the relative URL to link to user ID 15, gallery 12


// Generates: /users/15/gallery/12
<a href="<?= route_to('user_gallery', 15, 12) ?>">View Gallery</a>

setRule() :

This method sets a single rule. It takes the name of field as the first
parameter, an optional label and a string with a pipe-delimited list of rules
that should be applied:

$validation->setRule('username', 'Username', 'required');

The field name must match the key of any data array that is sent in. If the
data is taken directly from $_POST, then it must be an exact match for the
form input name

$validation->setRules([
'username' => 'required',
'password' => 'required|min_length[10]'
]);

Views Your views look much like before, but they are invoked differently ...
instead of CI3’s $this->load->view(x); you can use echo view(x); CI4
supports view “cells”, to build your response in pieces The template parser is
still there, but substantially enhanced

Alternative Echos
Normally to echo, or print out a variable you would do this:

<?php echo $variable; ?>


With the alternative syntax you can instead do it this way:
<?= $variable ?>

Alternative Control Structures


Controls structures, like if, for, foreach, and while can be written in a
simplified format as well. Here is an example using foreach:

<ul>
<?php foreach ($todo as $item) : ?>
<li> <?= $item ?> </li>
<?php endforeach ?>
</ul>

<?php if ($role=='admin'): ?>


<h1>Welcome, Admin!</h1>
<?php endif ?>

$this->validation = \Config\Services::validation();
$this->session = \Config\Services::session();
if ($bottom_content && !$content_only):
if (is_array($bottom_content)):
foreach ($bottom_content as $v) :
echo view($v);
endforeach;
else:
echo view($bottom_content);
endif;
endif;

helper(['form', 'url']);

<?php

namespace App\Models;

use CodeIgniter\Model;
class CountryModel extends Model {

public function selectCountries($columns = '*') {


return $this->db->table('countries')
->select($columns)
->orderBy('favorite DESC, name ASC')
->get()
->getResultArray();
}

public function selectCountry($id, $columns = '*') {


return $this->db->table('countries')
->select($columns)
->getWhere(['id' => $id])
->getRowArray();
}

public function getCountries() {


return $this->selectCountries();
}

public function getCounriesOptions() {


$options = [];
$countries = $this->selectCountries('id, name');
if (!empty($countries)) {
foreach ($countries as $country) {
$options[$country['id']] = $country['name'];
}
}

return $options;
}

public function getCountry($id) {


return $this->selectCountry($id);
}

public function getCountryName($id) {


return $this->selectCountry($id, 'name');
}

}
<?php if (file_exists(ROOTPATH . ASSETS . 'img/favicon.ico')): ?>
<link rel="icon" href="<?= ASSETS . 'img/favicon.ico' ?>" />
<?php endif; ?>
<?php if (isset($css_min) && !empty($css_min)): ?>
<?php echo trim($css_min) . "\n"; ?>
<?php else: ?>
<?php foreach ($css as $url): ?>
<link rel="stylesheet" type="text/css" href="<?php echo $url;
?>" />
<?php endforeach; ?>
<?php endif; ?>

You might also like