Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/techmahedy/zunoo

The Zuno Framework.
https://github.com/techmahedy/zunoo

framework mii php

Last synced: 2 months ago
JSON representation

The Zuno Framework.

Awesome Lists containing this project

README

        

## About Zuno

Zuno is a robust and versatile web application framework designed to streamline the development process and enhance productivity. It provides a comprehensive set of tools and features that cater to modern web development needs, integrating seamlessly with Adobe technologies to offer a cohesive development experience.

### Key Features

- **Route Management**: Zuno simplifies route definition and management, allowing developers to easily set up routes, handle parameters, and manage requests with minimal configuration. It supports both single and multiple route parameters to cater to complex routing needs.

- **Database Integration**: The framework offers powerful database tools, including support for migrations, seeding, and query building. Zuno’s database layer ensures efficient data management and interaction, with built-in support for models and database connections.

- **View Handling**: With Zuno, creating and managing views is straightforward. The framework supports custom Blade directives, allowing developers to extend view functionalities as needed.

- **Middleware Support**: Zuno includes support for both global and route-specific middleware, giving developers the flexibility to implement application-wide and route-specific logic easily.

- **Validation and Security**: The framework provides robust validation mechanisms and security features such as CSRF token management, ensuring that data handling and form submissions are both secure and reliable.

- **Dependency Injection**: Zuno promotes clean and maintainable code with its dependency injection system, supporting both constructor and method injection, and allowing for easy binding of interfaces to service classes.

- **Session and Logging**: Zuno offers comprehensive session management and logging capabilities, enabling developers to manage user sessions and track application logs effectively.

- **Collections and Macros**: The framework includes support for collections and custom macros, providing developers with additional tools to manipulate data and extend functionality as required.

Zuno is designed to be intuitive and developer-friendly, with a focus on providing the essential features needed for modern web applications while integrating seamlessly with Adobe technologies for enhanced capabilities.


# How to use

- [About](#section-1)
- [How to Install](#section-14)
- **Routes**
- [Define Route](#section-2)
- [Route Parameter](#section-8)
- [Multiple Route Parameters](#section-9)
- [Request](#section-15)
- **Database**
- [Database and Migration](#section-21)
- [Database Seeder](#section-22)
- [Database Query Builder](#section-23)
- [Model](#section-5)
- [Database Connection](#section-6)
- **Views**
- [Views](#section-7)
- [Custom Blade Directives](#section-11)
- **Middleware**
- [Global Middleware](#section-10)
- [Route Middleware](#section-17)
- **Validation**
- [Form Validation](#section-12)
- [CSRF Token](#section-16)
- **Collections**
- [Collection And Macro](#section-18)
- **Session and Logging**
- [Session And Flash Message](#section-19)
- [Log](#section-20)
- **Dependency Injection**
- [Binding Interface to Service Class](#section-3)
- [Controller Method Dependency Injection](#section-4)
- [Constructor Dependency Injection](#section-13)

## About
Zuno, A basic PHP MVC framework designed in a way that you feel like you are working in a Laravel application. In this framework, you will get all the basic features of a web application like routing, middleware, dependency injection, eloquent relationship, model, blade template engine interface injection, and many more. Test it and if you like, please give it a star.

## How to Install
We can easily set up and install this application with a few steps. Before using this application, a minimum `PHP 8.3` version is needed.
## Create a new project
`composer create-project zunoo/zunoo example-app`

- Step 1: Go to the project directory with this command `cd example-app`
- Step 2: Start the development server by running this command `php -S localhost:8000`

## Define Route
To define the route, navigate to this file and update
### `routes/web.php`
```php

## Model
Now look at Model, how you can use it
```php

'datetime',
];

/**
* Relationships
*
* Define any relationships this model has with other models. For example, if a user has many posts,
* you can define a relationship method here.
*
* Example:
* public function posts()
* {
* return $this->hasMany(Post::class);
* }
*/
}
```

## Database Connection
Connect your database like that, just pass your credentials to `.env`
```
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
```

And you can print configuration value like `$_ENV['DB_CONNECTION']` or you can use `env('DB_CONNECTION')`

## Views
To work with views, default view file path inside `resources/views`. Now passing data with views like
```php
{{ $version }}
```
### Avaiable blade systex
```BLADE
@section('looping-test')

Let's print odd numbers under 50:



@foreach($numbers as $number)
@if($number % 2 !== 0)
{{ $number }}
@endif
@endforeach


@endsection
```
For mastering template
```BLADE
@include('shared.header')


Welcome to {{ $title }}


{{ $content }}



Master file



@yield('looping-test')

@include('shared.footer')

```
`You can use any blade systex as you want like laravel framework`

## Route Parameters
You can pass single or multiple parameter with route as like below
```php

## Multiple Route Parameters
You can pass multiple parameter with route as like below
```php

## Request
Request is most important thing when we work in a web application. We can use Request in this application like
```php
has('name')){

}

//We can also check form request data like
if($request->has('name') && $request->has('email')){

}

//Now get the value from request like:
$name = $request->input('name');
$email = $request->input('email');

//You can also use global request() helper like:
$name = request()->input('name');

//or
if(request()->has('name')){

}

//get all the input as an array
$input = $request->all();
dd($input);
}
}

```

## Global Middleware
We can define multiple global middleware. To define global middleware, just update the `App\Http\Kernel.php` file's `$middleware` array as like below
```php

## Route Middleware
We can define multiple route middleware. To define route middleware, just update the `App\Http\Kernel.php` file's `$routeMiddleware` array as like below

```php

*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
];
```

And update your route like:

```php
middleware('auth');
```

Now update your middleware like

```php

## Database Query Builder
Zuno has its own custom query builder for fetching database query. See the very simple example

```php
select(['id', 'name', 'email'])
->where('status', '=', 'active')
->orderBy('name')
->limit(10)
->offset(0)
->get();

return $data; // this is laravel collection. you can use any collection wrapper in this data.

//you can check a data exists or not like that
$data = DB::table('users')
->where('status', '=', 'active')
->exists();

//you can also fetch first row of your table, it will return single collection
$data = DB::table('users')
->where('id', '=', 1)
->first();
}
}

```

## Custom Blade Directive
We can define custom blade directive. To define it, update `App\Providers\AppServiceProvider.php` as like below
```php

directive('capitalize', function ($text) {
return "";
});
}
}
```

And now we can call it in a blade file like
```HTML
{{ capitalize('hello') }}
```

## From Validation
We can validate from and can show error message in blade file very easily. To validate from , just assume we have two routes
```php
validate([
'email' => 'required|email|unique:users|min:2|max:100', //unique:users -> here [users] is table name
'password' => 'required|min:2|max:100',
]);

//save the data

return redirect()->url('/test'); //or redirect()->back()
}
}

```

Now update the `resources/user/index.blade.php` like
```HTML

@if (session()->has('errors'))
@foreach (session()->get('errors') as $error)
@foreach ($error as $item)

  • {{ $item }}

  • @endforeach
    @endforeach
    @endif

    @csrf


    Email address


    @if (session()->has('email'))
    {{ session()->get('email') }}
    @endif


    Password


    @if (session()->has('password'))
    {{ session()->get('password') }}
    @endif

    Submit

    ```

    ## CSRF Token
    If you submit a post request form, then you must be provide `csrf_token` with your request like below, otherwise it will throw an exception error.

    ```HTML

    @csrf

    ```

    ## Binding Interface to Service Class
    To bind the interface with your service class, just update `App\Providers\AppServiceProvider.php`.

    ```php
    input('payment_type')

    $this->bind(PaymentServiceContract::class, function() {
    return new StripePaymentService();
    });

    // Register any custom blade directives, macro or your own custom builds
    //
    // Place service bindings or provider registrations here.
    //
    // Example:
    // $this->bind(SomeService::class, function() {
    // return new SomeService();
    // });
    }
    }
    ```

    ## Dependency Injection
    Now look at that, how you can use dependency injection.
    ```php

    ## Constructor Dependency Injection
    Now look at that, how you can use dependency injection using constructor.
    ```php

    ## Collection & Macro
    Like Laravel framework, in this Zuno framework, you can also work with Laravel collection and you can create your own custom macro. To create a custom macro, just update service provider `App\Providers\AppServiceProvider.php` like:
    ```php
    map(function ($value) {
    return strtoupper($value);
    });
    });
    }
    }
    ```

    And now we can use it like:

    ```php
    toUpper();

    return $upper; //output ["FIRST","SECOND"]
    });
    ```

    ## Session Flash Message
    When we work with form submit then we need to show validation error message or success message. We can show session flash message very easily like
    ```php
    flash('key', 'value to be printed');

    //Now you can print this value lie
    if(session()->has('key')){
    echo session()->get('key');
    }

    ```

    ## Log
    We can easily print important messages in a log file which is located inside `storage\logs\Zuno.log`. To print a message, Zuno provide `logger()` helper function, you just need to follow this
    ```php
    info('Hello');

    ```

    ## Database and Migration
    Zuno allow you to create migration. To create migration, Zuno uses `CakePHP`'s `phinx`. So to create a migration file first you need to update the configuration file `environments` array like:
    ### `config.php`

    ```php
    [
    'default_migration_table' => 'phinxlog',
    'your_database_name' => [
    'adapter' => 'mysql',
    'host' => 'localhost',
    'name' => 'your_database_name',
    'user' => 'your_database_username',
    'pass' => 'your_database_password',
    'port' => '3306'
    ]
    ]
    ];
    ```
    Now run the below command in your project terminal like:
    `php vendor/bin/phinx create Post -c config.php`

    Here `Post` is the model name.

    Now this command will generate a migration file in the following path with the empty `change()` method.
    ### `app\database\migration\20231111144423_post.php`

    ```php
    table('posts');

    $table->addColumn('title', 'string', ['limit' => 20])
    ->addColumn('body', 'text')
    ->addColumn('cover_image', 'string')
    ->addTimestamps()
    ->addIndex(['title'], ['unique' => true]);

    $table->create();
    }
    }
    ```

    Now run the migration command:

    `php vendor/bin/phinx migrate -c config.php`.

    Now see the documentation of `phinx` [Documentation](https://book.cakephp.org/phinx/0/en/migrations.html) to learn more.

    ## Database Seeder
    Zuno allow you to create database seeder file to generate fake date. To create seeder, Zuno uses `CakePHP`'s `phinx`. So to create a seeder file first you need run below command. Assume we are going to create `PostSeeder`:

    Now run the below command in your project terminal like:
    `php vendor/bin/phinx seed:create PostSeeder -c config.php`

    Here `PostSeeder` is the seeder class name.

    Now this command will generate a seeder file in the following path with the empty `run()` method.
    ### `app\database\seeds\PostSeeder.php`

    ```php
    fake()->title(),
    'body' => fake()->title(),
    'created_at' => date('Y-m-d H:i:s'),
    ]
    ];

    $posts = $this->table('posts');
    $posts->insert($data)
    ->saveData();

    // empty the table
    // $posts->truncate();
    }
    }

    ```

    Now run the seeder command:

    `php vendor/bin/phinx seed:run -c config.php`.

    Or you can run specific seeder class file lie

    `php vendor/bin/phinx seed:run -s PostSeeder -c config.php`

    Now see the documentation from `phinx` [Documentation](https://book.cakephp.org/phinx/0/en/seeding.html) to learn more.

    ## Author Blog link:
    [Blog](https://www.laravelia.com)