• Tidak ada hasil yang ditemukan

CodeIgniter4 User Guide

N/A
N/A
gurmel singh

Academic year: 2024

Membagikan "CodeIgniter4 User Guide"

Copied!
761
0
0

Teks penuh

MySQL (5.1+) via the pdo driver Oracle via the oci8 and pdo drivers PostgreSQL via the pdo driver. ODBC via the odbc and pdo drivers (you should know that ODBC is actually an abstraction layer).

Installation

Manual Installation

Setup

Upgrading

Pros

Cons

Structure

Translations Installation

Composer Installation

The CodeIgniter 4 starter repository [https://github.com/codeigniter4/devstarter] maintains a skeleton app, just like the app launcher above, but with a. If you want to take advantage of system message translations, they can be added to your project in a similar way.

Git Installation

If you want to contribute to the system message translations, fork and clone the translations repository [https://github.com/codeigniter4/translations] separately from the codebase.

Coding Standards Installation

If you also want to use it within your project, composer requires codeigniter4/translations @beta.

Running Your App

This will start the server and you can now view your application in your browser at http://localhost:8080. You can use the --host CLI option to specify a different host to run the application at:. php spark serve --host=example.dev.

Virtual Hosting

Add a host alias in your "hosts" file, typically /etc/hosts on unix-type platforms or c:/Windows/System32/drivers/etc/hosts on Windows. If your project directory is not a subdirectory of the Apache document root, your element may need to have a nested element to allow the web server access to the files.

Testing

Your web app will be available at http://localhost:8080 with the code coverage report for your build at http://localhost:8081 and the user guide for it at http://localhost:8082.

Upgrading From a Previous Version

Upgrading from 3.x to 4.x

Controllers extend \CodeIgniter\Controller instead of CI_Controller. They no longer use a constructor (to call CI “magic”) unless that is part of a base controller you create. You don't need MY_x classes in your library folder to extend or replace CI4 components.

Troubleshooting

How do I know if my install is working?

I have to include index.php in my URL

Only the default page loads

The tutorial gives 404 errors everywhere :(

CodeIgniter Repositories

Composer Packages

Tutorial

Static pages

Let’s make our first controller

It also has an index() method, the same as the default controller in app/Controllers/Home.php; this method displays the CodeIgniter welcome page. The header contains the basic HTML code you want to display before loading the main view, along with the title.

Adding logic to the controller

The second parameter in the view() method is used to pass values ​​to the view. Each value in the $data array is assigned to a variable with the name of its key.

Running the App

The value of title is defined in this method, but instead of assigning the value to a variable, it is assigned to the title element of the $data array. The last thing to do is to load the views in the order they are.

Routing

Here, the second rule in the $routes array matches any request using the wildcard string (:any).

News section

Create a database to work with

It looked like he was writing a CodeIgniter app.' (2,'Say it isn't so!','say-it-isnt-so','Scientists conclude that some programmers have a sense of humor.'. 3,'caffeination, yes!','caffeine - yes', 'World's largest coffee bar open on site embedded coffee bar for staff only.'.

Connect to your database

Setting up your model

For this, the database abstraction layer included in CodeIgniter — Query Builder is used. The Model class also makes it easy to work with the query builder and provides some additional tools to simplify working with data.

Display the news

You can see that the $slug variable in the second method is passed to the model's method. The first line routes URIs with a slug to the view() method in the News controller.

Create news items

Create a form

If not, the form will be displayed; if it is submitted and all rules are met, the model is called.

Model Updating

Before you can start adding news to your CodeIgniter application, you need to add an additional rule to the app/Config/Routes.php file.

Congratulations

Conclusion

CodeIgniter4 Overview

Application Structure

Default Directories

Modifying Directory Locations

Models, Views, and Controllers

The Components

Views

Views get the data to display from the controllers, which pass it to the views as variables that can be displayed with simple echo calls. You can store the view file for this method in /app/Views/User/Profile.php.

Models

Views are generally stored in /app/Views, but can quickly become unwieldy if not organized in some way. Models are typically stored in /app/Models, although they can use a namespace to be grouped as you need.

Controllers

Autoloading Files

Configuration

Namespaces

Classmap

The class map is used extensively by CodeIgniter to squeeze the last bit of performance out of the system by not hitting the file system with extras. The key of each row is the name of the class you want to find.

Legacy Support

Composer Support

Services

When you want to extend or replace core classes, you just need to ensure that you meet the requirements of the interface and you know that the classes are compatible. There are occasions where you need to require only a single instance of a service to be created. It will contain a blog module with controllers, models, etc., and you would like to make some of the classes available as a service.

Working With HTTP Requests

What is HTTP?

The Request

This message displays all the information needed to know what the customer is looking for. It indicates the method for the request (GET, POST, DELETE, etc.) and the HTTP version it supports.

The Response

Working with Requests and Responses

The request class does a lot of the background work for you, so you don't have to worry about it. Additionally, the Response class allows you to work with the HTTP caching layer for best performance.

Security Guidelines

A1 Injection

OWASP recommendations

CodeIgniter provisions

A2 Weak authentication and session management

A3 Cross Site Scripting (XSS)

A4 Insecure Direct Object Reference

A5 Security Misconfiguration

A6 Sensitive Data Exposure

A7 Missing Function Level Access Control

A8 Cross Site Request Forgery (CSRF)

A9 Using Components with Known Vulnerabilities

A10 Unvalidated Redirects and Forwards

Presentation: do not use URL redirection; use random indirect references Controller: do not use URL redirection; use random indirect references Model: validate roles.

General Topics

Working With Configuration Files

You can find the configuration files anywhere on your server using a different namespace. If the prefix of a name-spaced variable matches the configuration class name exactly, case sensitive, then the trailing part of the variable name (after the dot) is treated as a configuration property name. If it matches an existing configuration property, the environment variable's value will override the corresponding one in the configuration file.

CodeIgniter URLs

URI Segments

Removing the index.php file

Apache Web Server

NGINX

Helper Functions

Where name is the file name of the helper, without the .php file extension or the “helper” part. You would prefix the name of the Helper with the namespace in which it can reside. Once you've loaded the Helper file containing the function you want to use, call it the same way you would a standard PHP function.

Global Functions and Constants

Parameters: $filename (string|array) – The name of the auxiliary file to load, or an array of names. Returns: An instance of the Session object if there is no $key, the value found in the session for $key, or null. Identical to the service() function described above, except that all calls to this function will return a new instance of the class, with the service returning the same instance each time.

Logging Information

In /app/Config/Logger.php you can change which levels are actually logged and assign different loggers to handle different levels. To log an exception or error, you can use a key. You can use any other recorder you like as long as it comes from.

Error Handling

This can be handy if you want to debug yourself or perform a cleanup before the script ends. You can change this by looking for the environment configuration section at the top of the main index.php file. You can pass a message to the exception that will be displayed instead of the default message on the 404 page.

Web Page Caching

How Does Caching Work?

Enabling Caching

If you change configuration settings that may affect your output, you must manually delete your cache files. Before the cache files can be written, you need to set the cache engine by editing app/Config/Cache.php.

Deleting Caches

Code Modules

You can use the $enabled class variable to turn on or off all automatic detections on the system. If an element is not present, then no autodiscovery will occur for that element, but others in the array will still be discovered. Some of this information is described in more detail in the appropriate place in the owner's manual, but it is shown here to help you understand how all the parts fit together.

Managing your Applications

Renaming or Relocating the Application Directory

Running Multiple Applications with one CodeIgniter Installation

If you want to share a common CodeIgniter framework installation, to manage several different applications, simply put all the directories located in your application directory into their own (sub)directory. It will have two applications, "foo" and "bar", both of which have standard application directories and a public folder, and share a common code launcher. If one of the applications had a command-line component, you would also change spark in each application's project file, as indicated above.

Handling Multiple Environments

The ENVIRONMENT Constant

Apache

Boot Files

Effects On Default Framework Behavior

Error Reporting

Configuration Files

Controllers and Routing

In the example above, CodeIgniter would try to find a controller called Blog.php and load it. Now if you load your main index.php file without specifying any URI segments, you will see the message "Hello World" by default. Simply drop in a controller that matches the name of your 'default_controller' as specified in your app/Config/Routes.php file.

URI Routing

The resource() method allows you to quickly create a handful of RESTful routes for a single resource. The routes are matched in the order they are specified, so if you have a source photo above a 'photos/poll', the show action route for the source line will be matched before the get line. Although a default namespace is added to the generated controllers (see below), the namespace option also allows you to specify a different namespace to be used in each option array.

Controller Filters

From any filter you can return the $request object and it will replace the current request, allowing you to make changes that will still be present when the controller runs. After filters are almost identical to before filters, except that you can only return the $response object and you cannot stop script execution. You can apply filters to all requests from a particular HTTP method, such as POST, GET, PUT, etc.

HTTP Messages

What is Content Negotiation?

Class Reference

If the header has multiple values, the values ​​will be returned as an array of values. This method allows you to easily get a string representation of header values ​​when the header has multiple values. If no match can be made between the header values ​​and the supported values, the first element of the array will be returned.

Request Class

Takes an IP address as input and returns true or false (boolean) depending on whether it is valid or not. Accepts an optional second string parameter of 'ipv4' or 'ipv6' to specify an IP format. Parameters: $upper (bool) – Whether to return the name of the request method in upper or lower case.

IncomingRequest Class

You can also check whether the request was made over an HTTPS connection with the isSecure() method:. You can access any header sent with the request using the . You can retrieve a URI object that represents the current URI for this request via the $request->uri property.

Content Negotiation

Loading the Class

Negotiating

Media

Language

Encoding

Character Set

HTTP Method Spoofing

Building Responses

In fact, views can be flexibly nested within other views (within other views, etc.) if you need this type of hierarchy. Remember that in MVC, the controller acts as a traffic policeman, so it is responsible for fetching a particular view. Using the controller instance you created on the controller side, let's add a view to it.

Referensi

Dokumen terkait

RE:PHP Framework Berbasis Objek - [b]PHALCON FRAMEWORK[/b] Phalcon adalah framework PHP yang biasa digunakan untuk membuat aplikasi web, Kelebihan Phalcon dibanding framework

If you see any of the following access icons above each content link when you search for content, you should be able to access the relevant article or book chapter....

Ebrary User Guide Downloading a Book: Reading ebrary books online: Accessing BookShelf: Using the Bookshelf: ebrary Mobile App Installation and Sign in procedure: Using

Quick Start Guide iScored.today Quick Start Guide: Using iScored.today Full documentation can be found at Archers Diary Support: http://support.archersdiary.com Begin Scoring Press

Financial Management Information System User Guide Process ID TSFM50007 Process Name Create Journal Entries Information System Financial Management Information System

Companies, News and Deals Video Tutorial: https://bcove.video/330HWM5 Search and Browse Video Tutorial: https://bcove.video/2OeQE61 Country Statistics User Guide:

User Guide Free Access – Free access to all users Full Access – available via a current subscription Open Access – available as an OA article HOMEPAGE Anyone can browse, search and

A comprehensive guide to the principles and applications of