수색…


매우 간단한 Hello World 응용 프로그램

Codeigniter 3의 새로 설치를 시작으로 여기에 Hello World 애플리케이션으로 시작하여이 견고한 PHP 프레임 워크로 얼음을 깨는 간단한 방법이 있습니다.

이렇게하려면 Hello World 앱에 표시 할보기를 만들기 시작할 수 있습니다.

우리는 그것을 당신의 어플리케이션 폴더에 넣을 것입니다 :

hello_world.php ( /application/views/ )에서

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Hello World</title>
</head>
<body>

    <h1>Hello World!</h1>

</body>
</html>

그것은 단순한 HTML 콘텐츠입니다.

이제이 뷰를 보여주기 위해 컨트롤러 가 필요합니다. 콘트롤러는 콘텐트가 디스플레이 될 수 있도록 뷰를 불러올 콘트롤러이다.

컨트롤러가 제대로 작동하려면 컨트롤러가 적절한 컨트롤러 폴더에 있어야합니다.

Hello World 컨트롤러를 배치 할 곳은 다음과 같습니다.

/application/controllers/Hello_world.php

(컨트롤러의 이름은 대개 첫 문자가 대문자 인 snake_case 입니다)

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Hello_world extends CI_Controller {

    public function __construct()
    {
    parent::__construct();
    }

    public function index(){
        $this->load->view('hello_world');
    }

}

컨트롤러의 기본 기능은 인덱스 기능입니다.

이제 다음 주소에 액세스하는 Hello World 페이지의 내용을 볼 수 있습니다.

http://[your_domain_name]/index.php/hello_world

또는 .htaccess를 사용하여 수정 프로그램을 적용한 경우 (수정 프로그램의 설치 페이지로 돌아 가기)

http://[your_domain_name]/hello_world

(로컬로 작업하는 경우 페이지를 찾을 수있는 주소는 http://localhost/hello_world )

URL은 실제로 컨트롤러 클래스 (이 경우 Hello_world 이지만 URL의 모든 소문자 사용)를 호출하여 형성됩니다. 이 경우 인덱스 함수를 사용 했으므로 충분합니다. 다른 함수 이름 ( greetings )을 사용했다면 다음과 같은 URL을 사용해야합니다.

http://[your_domain_name]/hello_world/greetings

/[controller_name]/[method_name] 으로 구조화되어 있습니다.

여기 있네! 귀하의 첫 번째 Codeigniter 응용 프로그램이 작동 중입니다!

컨트롤러를 조금 더 사용 해보자.

이제 컨트롤러의 기능을 사용하여보기를 채우는 좀 더 복잡한 예를 시도해 보겠습니다.

다음은 우리의 관점입니다 : /application/views/hello_world.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Hello World</title>
</head>
<body>

<h1><?php echo $greetings;?></h1>

</body>
</html>

이제 인사말을 표시 할 자리 표시자가 있습니다.

이 작업을 수행하기 위해 컨트롤러를 변경하는 방법은 다음과 같습니다.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Hello_world extends CI_Controller {
    
    public function __construct() {
        parent::__construct();
    }

    public function greetings(){
        $data = array('greetings'=>'Hello World');
        $this->load->view('hello_world',$data);
    }
}

$data 배열은 뷰 내부에 리콜 된 동일한 레이블 ( greetings )을 사용하여 뷰에 삽입 할 정보를 준비합니다.

최종 결과는 첫 번째 예제와 동일하지만 이제 프레임 워크의 잠재력을 더 많이 사용하고 있습니다!

인사말을 선택하겠습니다 : Hello World 또는 Good Bye World 또는 ...?

다른 URL을 통해 액세스 할 수있는 대체 인사말을 원한다고 가정 해 봅시다. 새로운 기능을 만들거나 새로운 컨트롤러를 만들 수도 있지만 가장 좋은 방법은 기존 제품을 최적화하여 최상의 성능을 발휘하도록하는 것입니다.

이를 위해 앞의 예제와 동일한 뷰를 유지하지만 두 개의 다른 인사말 중에서 하나를 선택할 수 있도록 함수에 매개 변수를 소개합니다.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Hello_world extends CI_Controller {
    
    public function __construct() {
        parent::__construct();
    }

    public function greetings($my_greetings){
        switch($my_greetings)
        {
            case 'goodbye':
                $say = 'Good Bye World';
            break;
            case 'morning':
                $say = 'Good Morning World';
            break;
            default:
                $say = 'Hello World';
        }
        $data = array('greetings'=>$say);
        $this->load->view('hello_world',$data);
    }
}

이제 여러 가지 인사말 옵션이 제공됩니다. 매개 변수를 시각화하기 위해 다음과 같이 매개 변수를 URL에 추가합니다.

http://[your_domain_name]/hello_world/greetings/goodbye

이렇게하면 우리에게 "안녕히 계십시오."라는 메시지가 표시됩니다.

URL의 구조는 다음과 같습니다.

http://[your_domain_name]/[controller_name]/[function_name]/[parameter_1]

이 경우 이전의 "Hello World"로 되돌아 가려면 매개 변수없이 이전 URL을 호출하는 것으로 충분합니다.

http://localhost/hello_world/greetings

함수에 여러 개의 매개 변수를 추가 할 수 있습니다 (예 : 3 개가 필요한 경우).

public function greetings($param1,$param2,$param3)

다음과 같이 URL을 사용하여 채울 수 있습니다.

http://[your_domain_name]/[controller_name]/[function_name]/[param1]/[param2]/[param3]

예 : http://localhost/hello_world/greetings/goodbye/italian/red

이렇게하면 표시 될 내용에 영향을주는 매개 변수를 URL에서 직접 전달할 수 있습니다.

URL을 통해 매개 변수를 전달하는 방법에 대한 자세한 내용을 보려면 라우팅 주제를 살펴보십시오.



Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow