PHP code example of overtrue / yaf-skeleton

1. Go to this page and download the library: Download overtrue/yaf-skeleton library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

overtrue / yaf-skeleton example snippets





class ExampleController extends BaseController
{
    public function handle()
    {
        return 'Hello world!';
        // return json(['foo' => 'bar']);
        // return redirect('https://easywechat.com');
        // return view('welcome', ['name' => 'MyApp']); // template engine 

    public function handle()
    {
        $data = [
            'name' => 'overtrue',
            'age' => 28,
        ];

        return view('profile-page', $data);
    }

$controller = mock_controller(Foo_BarController::class);

// Indicates the method to mock, and the protected method is also mockable
$controller = mock_controller(Foo_BarController::class, ['getUsers', 'getApp']);

    ...
    public function handle()
    {
        $params = Request::only('uids', 'screen_name', 'trim_status', 'has_extend', 'simplify', 'is_encoded');

        $users = $this->get('main.users.show_batch', $params);

        return $users;
    }
    ...

public function testHandle()
{
    $input = [
        'uids' => '2193182644',
        'screen_name' => '安正超',
        'trim_status' => 0,
        'has_extend' => 1,
        'simplify' => 0,
        'is_encoded' => 0,
        'foo' => 'bar',
    ];

    Request::shouldReceive('only')
            ->with('uids', 'screen_name', 'trim_status', 'has_extend', 'simplify', 'is_encoded')
            ->andReturn($input);

    $controller = mock_controller(Users_Show_BatchController::class, ['get']); // mock the `get` method

    $controller->shouldReceive('get')->with('main.users.show_batch', array_except($_GET, ['foo']))
                                    ->andReturn('foo')
                                    ->once();

    $response = $controller->handle();

    $this->assertSame('foo', $response);
}

Request::shouldReceive('get')->with('mid')->andReturn('mock-mid')->once();
Log::shouldReceive('action')->with(48, 'oid', 'ext')->once();
..

$this->shouldAbort($message, $code);

$this->method('post');

$this->uri('/foo/bar?uid=12345');

$this->config('foo', 'bar');

$this->ip('127.0.0.1');

$this->server('REQUEST_URI', '/foo/bar');
shell
$ ./sora make:controller Foo_Bar # or:foo_bar/FooBar/FooBarController
#
# /www/myapp/app/controllers/Foo/Bar.php Created!
# /www/myapp/tests/controllers/Foo/BarTest.php Created!
shell
$ ./sora make:test Foo_Bar # Also supports multiple type controller names
# /www/myapp/tests/controllers/Foo/BarTest.php Created!
html
<!--app/views/profile-page.php:-->

<h1><?= $name