Hướng dẫn php klein tutorial - hướng dẫn php nhỏ

Klein.php

Klein.php là một bộ định tuyến nhanh và linh hoạt cho PHP 5.3+ is a fast & flexible router for PHP 5.3+

  • Định tuyến biểu hiện chính quy linh hoạt (lấy cảm hứng từ Sinatra)
  • Một tập hợp các phương thức nồi hơi để xây dựng nhanh các ứng dụng web
  • Hầu như không có chi phí => 2500+ yêu cầu/giây

Bắt đầu

  1. Php 5.3.x là bắt buộc
  2. Cài đặt Klein bằng Trình soạn thảo (được đề xuất) hoặc thủ công
  3. Thiết lập viết lại url để tất cả các yêu cầu được xử lý bởi index.phpindex.php
  4. (Tùy chọn) Ném vào một số APC để có biện pháp tốt

Cài đặt nhà soạn nhạc

  1. Nhận nhà soạn nhạc
  2. Yêu cầu klein với
    $klein->respond('/[:name]', function ($request) {
        return 'Hello ' . $request->name;
    });
    1
  3. Thêm thông tin sau vào tệp PHP chính của ứng dụng của bạn:
    $klein->respond('/[:name]', function ($request) {
        return 'Hello ' . $request->name;
    });
    2

Thí dụ

Hello World - Bắt buộc xin chào Thế giới bắt buộc

<?php
require_once __DIR__ . '/vendor/autoload.php';

$klein = new \Klein\Klein();

$klein->respond('GET', '/hello-world', function () {
    return 'Hello World!';
});

$klein->dispatch();

Ví dụ 1 - Trả lời tất cả các yêu cầu

$klein->respond(function () {
    return 'All the things';
});

Ví dụ 2 - tham số được đặt tên

$klein->respond('/[:name]', function ($request) {
    return 'Hello ' . $request->name;
});

Ví dụ 3 - thật yên tĩnh

$klein->respond('GET', '/posts', $callback);
$klein->respond('POST', '/posts', $callback);
$klein->respond('PUT', '/posts/[i:id]', $callback);
$klein->respond('DELETE', '/posts/[i:id]', $callback);
$klein->respond('OPTIONS', null, $callback);

// To match multiple request methods:
$klein->respond(array('POST','GET'), $route, $callback);

// Or you might want to handle the requests in the same place
$klein->respond('/posts/[create|edit:action]?/[i:id]?', function ($request, $response) {
    switch ($request->action) {
        //
    }
});

Ví dụ 4 - Gửi đối tượng / tệp

$klein->respond(function ($request, $response, $service) {
    $service->xml = function ($object) {
        // Custom xml output function
    }
    $service->csv = function ($object) {
        // Custom csv output function
    }
});

$klein->respond('/report.[xml|csv|json:format]?', function ($request, $response, $service) {
    // Get the format or fallback to JSON as the default
    $send = $request->param('format', 'json');
    $response->$send($report);
});

$klein->respond('/report/latest', function ($request, $response, $service) {
    $response->file('/tmp/cached_report.zip');
});

Ví dụ 5 - tất cả cùng nhau

$klein->respond(function ($request, $response, $service, $app) use ($klein) {
    // Handle exceptions => flash the message and redirect to the referrer
    $klein->onError(function ($klein, $err_msg) {
        $klein->service()->flash($err_msg);
        $klein->service()->back();
    });

    // The fourth parameter can be used to share scope and global objects
    $app->db = new PDO(...);

    // $app also can store lazy services, e.g. if you don't want to
    // instantiate a database connection on every response
    $app->register('db', function() {
        return new PDO(...);
    });
});

$klein->respond('POST', '/users/[i:id]/edit', function ($request, $response, $service, $app) {
    // Quickly validate input parameters
    $service->validateParam('username', 'Please enter a valid username')->isLen(5, 64)->isChars('a-zA-Z0-9-');
    $service->validateParam('password')->notNull();

    $app->db->query(...); // etc.

    // Add view properties and helper methods
    $service->title = 'foo';
    $service->escape = function ($str) {
        return htmlentities($str); // Assign view helpers
    };

    $service->render('myview.phtml');
});

// myview.phtml:
<title><?php echo $this->escape($this->title) ?></title>

Không gian tên tuyến đường

$klein->with('/users', function () use ($klein) {

    $klein->respond('GET', '/?', function ($request, $response) {
        // Show all users
    });

    $klein->respond('GET', '/[:id]', function ($request, $response) {
        // Show a single user
    });

});

foreach(array('projects', 'posts') as $controller) {
    // Include all routes defined in a file under a given namespace
    $klein->with("/$controller", "controllers/$controller.php");
}

Các tệp được bao gồm được chạy trong phạm vi của Klein (

$klein->respond('/[:name]', function ($request) {
    return 'Hello ' . $request->name;
});
3) vì vậy tất cả các phương thức/thuộc tính Klein có thể được truy cập bằng
$klein->respond('/[:name]', function ($request) {
    return 'Hello ' . $request->name;
});
4

Tệp ví dụ cho: "Bộ điều khiển/Project.php"

// Routes to "/projects/?"
$this->respond('GET', '/?', function ($request, $response) {
    // Show all projects
});

Dịch vụ lười biếng

Các dịch vụ có thể được lưu trữ một cách lười biếng, có nghĩa là chúng chỉ được khởi tạo khi sử dụng đầu tiên.lazily, meaning that they are only instantiated on first use.

<?php
$klein->respond(function ($request, $response, $service, $app) {
    $app->register('lazyDb', function() {
        $db = new stdClass();
        $db->name = 'foo';
        return $db;
    });
});

//Later

$klein->respond('GET', '/posts', function ($request, $response, $service, $app) {
    // $db is initialised on first request
    // all subsequent calls will use the same instance
    return $app->lazyDb->name;
});

Người xác nhận

Để thêm trình xác thực tùy chỉnh sử dụng

$klein->respond('/[:name]', function ($request) {
    return 'Hello ' . $request->name;
});
5

$service->addValidator('hex', function ($str) {
    return preg_match('/^[0-9a-f]++$/i', $str);
});

Bạn có thể xác nhận các tham số bằng cách sử dụng

$klein->respond('/[:name]', function ($request) {
    return 'Hello ' . $request->name;
});
6 hoặc
$klein->respond('/[:name]', function ($request) {
    return 'Hello ' . $request->name;
});
7, ví dụ:

$klein->respond(function () {
    return 'All the things';
});
0

Hoặc bạn có thể xác thực bất kỳ chuỗi nào bằng cùng một luồng ..

$klein->respond(function () {
    return 'All the things';
});
1

Các phương thức xác thực có thể chuỗi và một thông báo ngoại lệ tùy chỉnh có thể được chỉ định cho nếu/khi xác thực không thành công

$klein->respond(function () {
    return 'All the things';
});
2

Lộ trình

[match_type: param_name] match_type : param_name ]

Vài ví dụ

$klein->respond(function () {
    return 'All the things';
});
3

Một số ví dụ phức tạp hơn

$klein->respond(function () {
    return 'All the things';
});
4

Lưu ý - Tất cả các tuyến khớp khớp với URI yêu cầu được gọi - điều này cho phép bạn kết hợp logic có điều kiện phức tạp như xác thực người dùng hoặc bố cục xem. ví dụ. Ví dụ cơ bản, mã sau đây sẽ bọc các tuyến khác với tiêu đề và chân trang - all routes that match the request URI are called - this allows you to incorporate complex conditional logic such as user authentication or view layouts. e.g. as a basic example, the following code will wrap other routes with a header and footer

$klein->respond(function () {
    return 'All the things';
});
5

Các tuyến đường tự động khớp với toàn bộ URI yêu cầu. Nếu bạn chỉ cần khớp một phần của URI yêu cầu hoặc sử dụng biểu thức thông thường tùy chỉnh, hãy sử dụng toán tử

$klein->respond('/[:name]', function ($request) {
    return 'Hello ' . $request->name;
});
8. Nếu bạn cần phủ định một tuyến đường, hãy sử dụng toán tử
$klein->respond('/[:name]', function ($request) {
    return 'Hello ' . $request->name;
});
9

$klein->respond(function () {
    return 'All the things';
});
6

Lượt xem

Bạn có thể gửi các thuộc tính hoặc người trợ giúp đến chế độ xem bằng cách gán chúng cho đối tượng

$klein->respond('GET', '/posts', $callback);
$klein->respond('POST', '/posts', $callback);
$klein->respond('PUT', '/posts/[i:id]', $callback);
$klein->respond('DELETE', '/posts/[i:id]', $callback);
$klein->respond('OPTIONS', null, $callback);

// To match multiple request methods:
$klein->respond(array('POST','GET'), $route, $callback);

// Or you might want to handle the requests in the same place
$klein->respond('/posts/[create|edit:action]?/[i:id]?', function ($request, $response) {
    switch ($request->action) {
        //
    }
});
0 hoặc bằng cách sử dụng arg thứ hai của
$klein->respond('GET', '/posts', $callback);
$klein->respond('POST', '/posts', $callback);
$klein->respond('PUT', '/posts/[i:id]', $callback);
$klein->respond('DELETE', '/posts/[i:id]', $callback);
$klein->respond('OPTIONS', null, $callback);

// To match multiple request methods:
$klein->respond(array('POST','GET'), $route, $callback);

// Or you might want to handle the requests in the same place
$klein->respond('/posts/[create|edit:action]?/[i:id]?', function ($request, $response) {
    switch ($request->action) {
        //
    }
});
1

$klein->respond(function () {
    return 'All the things';
});
7

myview.phtml

$klein->respond(function () {
    return 'All the things';
});
8

Các chế độ xem được biên dịch và chạy trong phạm vi của

$klein->respond('GET', '/posts', $callback);
$klein->respond('POST', '/posts', $callback);
$klein->respond('PUT', '/posts/[i:id]', $callback);
$klein->respond('DELETE', '/posts/[i:id]', $callback);
$klein->respond('OPTIONS', null, $callback);

// To match multiple request methods:
$klein->respond(array('POST','GET'), $route, $callback);

// Or you might want to handle the requests in the same place
$klein->respond('/posts/[create|edit:action]?/[i:id]?', function ($request, $response) {
    switch ($request->action) {
        //
    }
});
0 để tất cả các phương thức dịch vụ có thể được truy cập bằng
$klein->respond('/[:name]', function ($request) {
    return 'Hello ' . $request->name;
});
4

$klein->respond(function () {
    return 'All the things';
});
9

API

Dưới đây là danh sách các phương thức công khai trong các lớp chung mà bạn có thể sẽ sử dụng. Để biết nguồn tài liệu lớp/phương thức chính thức hơn, vui lòng xem tài liệu được tạo của PHPDOC.

$klein->respond('/[:name]', function ($request) {
    return 'Hello ' . $request->name;
});
0

Kiểm tra đơn vị

Các thử nghiệm đơn vị là một phần quan trọng của việc phát triển một công cụ định tuyến như Klein. Các tính năng hoặc sửa lỗi được thêm vào có thể có các tác dụng phụ khó tìm mà không cần thử nghiệm nhiều, do đó tầm quan trọng của việc kiểm tra đơn vị.

Dự án này sử dụng PHPUNIT làm khung thử nghiệm đơn vị của nó.

Các bài kiểm tra đều sống trong

$klein->respond('GET', '/posts', $callback);
$klein->respond('POST', '/posts', $callback);
$klein->respond('PUT', '/posts/[i:id]', $callback);
$klein->respond('DELETE', '/posts/[i:id]', $callback);
$klein->respond('OPTIONS', null, $callback);

// To match multiple request methods:
$klein->respond(array('POST','GET'), $route, $callback);

// Or you might want to handle the requests in the same place
$klein->respond('/posts/[create|edit:action]?/[i:id]?', function ($request, $response) {
    switch ($request->action) {
        //
    }
});
4 và mỗi bài kiểm tra mở rộng một lớp trừu tượng
$klein->respond('GET', '/posts', $callback);
$klein->respond('POST', '/posts', $callback);
$klein->respond('PUT', '/posts/[i:id]', $callback);
$klein->respond('DELETE', '/posts/[i:id]', $callback);
$klein->respond('OPTIONS', null, $callback);

// To match multiple request methods:
$klein->respond(array('POST','GET'), $route, $callback);

// Or you might want to handle the requests in the same place
$klein->respond('/posts/[create|edit:action]?/[i:id]?', function ($request, $response) {
    switch ($request->action) {
        //
    }
});
5

Để kiểm tra dự án, chỉ cần chạy

$klein->respond('GET', '/posts', $callback);
$klein->respond('POST', '/posts', $callback);
$klein->respond('PUT', '/posts/[i:id]', $callback);
$klein->respond('DELETE', '/posts/[i:id]', $callback);
$klein->respond('OPTIONS', null, $callback);

// To match multiple request methods:
$klein->respond(array('POST','GET'), $route, $callback);

// Or you might want to handle the requests in the same place
$klein->respond('/posts/[create|edit:action]?/[i:id]?', function ($request, $response) {
    switch ($request->action) {
        //
    }
});
6 để tải xuống phiên bản Phpunit chung với trình soạn thảo và chạy các bài kiểm tra từ thư mục chính với
$klein->respond('GET', '/posts', $callback);
$klein->respond('POST', '/posts', $callback);
$klein->respond('PUT', '/posts/[i:id]', $callback);
$klein->respond('DELETE', '/posts/[i:id]', $callback);
$klein->respond('OPTIONS', null, $callback);

// To match multiple request methods:
$klein->respond(array('POST','GET'), $route, $callback);

// Or you might want to handle the requests in the same place
$klein->respond('/posts/[create|edit:action]?/[i:id]?', function ($request, $response) {
    switch ($request->action) {
        //
    }
});
7

Đóng góp

Xem Hướng dẫn đóng góp để biết thêm thông tin

Thêm thông tin

Xem Wiki để biết thêm thông tin

Người đóng góp

  • Trevor N. Suarez

Giấy phép

(Giấy phép MIT)

Bản quyền (c) 2010 Chris O'Hara

Quyền được cấp, miễn phí, cho bất kỳ ai có được bản sao phần mềm này và các tệp tài liệu liên quan ("phần mềm"), để giải quyết phần mềm mà không bị hạn chế, bao gồm không giới hạn quyền sử dụng, sao chép, sửa đổi, hợp nhất , Xuất bản, Phân phối, Bán cấp và/hoặc Bán các bản sao của phần mềm và cho phép những người mà phần mềm được cung cấp để làm như vậy, tuân theo các điều kiện sau:

Thông báo bản quyền trên và Thông báo quyền này sẽ được bao gồm trong tất cả các bản sao hoặc các phần đáng kể của phần mềm.

Phần mềm được cung cấp "như là", không có bảo hành dưới bất kỳ hình thức nào, rõ ràng hay ngụ ý, bao gồm nhưng không giới hạn trong các bảo đảm của thương mại, thể lực cho một mục đích cụ thể và không bị thiếu sót. Trong mọi trường hợp, các tác giả hoặc chủ bản quyền sẽ phải chịu trách nhiệm đối với bất kỳ khiếu nại, thiệt hại hoặc trách nhiệm nào khác, cho dù trong hành động của hợp đồng, tra tấn hay nói cách khác, phát sinh từ hoặc liên quan đến phần mềm hoặc việc sử dụng hoặc các giao dịch khác trong PHẦN MỀM.