makechtec/nanokit

library for php proyects

dev-main 2021-09-05 02:42 UTC

This package is auto-updated.

Last update: 2024-05-05 08:26:36 UTC


README

This is a library open source it have simplies functionalities for develop an small web php application.

requirements

First you need have installed php.

get started

1. Register a route

Supposed you have php installed in the global environement, go to the app/routes.php file and register a new route to receive like:

`Route::get( 'home', [ HomeController::class, 'home' ] );`

the Route::get() function register a uri and bind this with the controller function passed as second parameter.

2. create a controller with his function

Go to src/Controllers directory and create a new file with the controller class name like HomeController, then define a function that will be called when the registered uri is called. The namespace should be App\Controllers.

<?php
namespace App\Controllers;

class HomeController{
    public function home(){
        // your code here
    }
}

3. Redirecting to a view.

use the global function view() for insert a view file inside the controller, this function also supports send parameters like an array.

public function home(){
    view( 'home', [ 'firstParam' => 'hello world' ] );
}

Then, create a view file in the src/Views, and add custom php or html code, also you can get the sended parameter from the controller.

<?php echo( $firstParam ); ?>

4. start the development server.

Open a terminal and enter in your current directory, then use the next command:

php composer.phar dump-autoload

This command register all your class files created and remove the requirement of use include() function.

Then use:

php -S localhost:8000 -t public/

This command start a development server in the localhost with the 8000 port, the root directory called is inside public/.

you should see something like:

hello world

Uri's with parameters

For catch GET parameters you need register the route defining his names using curly brackets.

Route::get( 'home/{user}/dashboard/{resource}' );

In your controller you only need receive it like a function parameter.

public function home( $user, $resource ){