Skip to content

Small php MVC Framework that has all it needs to create small app.

License

Notifications You must be signed in to change notification settings

you97ssef/Yaravel

Repository files navigation

Yaravel

Yaravel is a small, yet robust PHP MVC framework that can be used for both rendering HTML and as an API. It features a request and routing system, a view engine with support for stylesheets and javascript, and an ORM called Helloquent for database communication. Yaravel is designed to be fast, efficient and flexible, making it an ideal choice for developers looking to build web applications with PHP.

Tweeter is a twitter like app built using an early version of this framework it allows users to post short messages, known as "tweets", and interact with others through likes and checking out their tweets. The app features a real-time feed of the recent tweets from users.
(check it out live or the the repo of this app)

Routing

Your application routes can be registered in the "/routes" folder. API routes should be placed in the "api.php" file and web pages in the "web.php" file.

Web Routes

To register a web route, use the addRoute function on the router object. The function takes the following parameters:

  • path: the path after the domain name
  • controller path: the path to the controller file
  • action: the function in the controller that should be executed
// $router->addRoute(path, controller path, action);
$router->addRoute("/", DefaultController::class, "viewDefault");

Refer to the examples in "/routes/web.php" for more information.

API Routes

To register an API route, use the api function on the router object. The function takes the following parameters:

  • path: the path after the domain name
  • controller path: the path to the controller file
  • method (optional, default is "GET"): the request method (can be "POST", "PUT" or "DELETE"). The controller action will have the same name as the method.
// $router->addRoute(path, controller path, method);
$router->api("/", DefaultController::class);
$router->api("/", DefaultController::class, "post");

Refer to the examples in "/routes/api.php" for more information.

Request

The $this->request object is included inside the controllers and is an instance of the HttpRequest class. It holds all the data from the request. The object has several properties, including:

  • get: an array that stores the data sent through parameters.
  • post: an array that stores the data sent through a POST request.
  • method: the method used in the request.
  • script_name: the path of the current script.
  • root: the root directory of the application.
  • path_info: the path information trailing the domain name.

Response

The HttpResponse class is used to respond to API calls. It has a static function called respond which has two parameters:

  • content: the data that will be returned to the client.
  • status:(optional) the response status, which should be selected from the predefined statuses in the status class. The default status is 200 OK.

MVC

MVC (Model-View-Controller) is a software design pattern that separates an application into three main components

Model

The Model component in the framework is responsible for handling data and business logic, and for communicating with the database. This is done through the use of Helloquent, an ORM tool specifically built for the framework. For further details, refer to the Helloquent documentation in this link https://github.com/you97ssef/Helloquent.
Models should be stored in the /app/Models directory and should extend the Model class (as demonstrated by the Person class in the code).

View

The View component contains functions that return HTML mixed with data to be displayed to the user. The data used can be passed from the Controller and can be accessed via the $this->data attribute(array). Views should be stored in the /app/views directory and should extend the View class (as demonstrated by the DefaultView or ErrorView classes in the code).

Controller

Revised Text: The Controller component handles user input and updates the Model and View accordingly. Controllers should be stored in the /app/Controllers directory and should extend the Controller class. It has a property $this->request that contains data from the incoming request. To respond as a web page, a View should be defined and the render function of that View should be called, such as $view->render("renderBody"). To respond to an API call, the respond function from the HttpResponse class should be used, for example HttpResponse::respond($data). See the DefaultController for an example.

Further information

The application starts with the index.php file. Firstly, the request is constructed, followed by the registration of CSS and JS. Next, the app configuration and database configuration are initialized, and then the router is set up. Finally, the app is executed by calling $router->run(); (see index.php file for more info)

This framework is licensed under the MIT License, which is a permissive open-source license that allows for the use, modification, and distribution of the software. The MIT License allows for a high degree of flexibility in how the software can be used and incorporates a minimal set of restrictions.


© Yaravel 2023 - by Youssef

Releases

No releases published

Packages

No packages published

Languages