Understand how the views work in Laragine (a package in laravel)
Tags: laravel, blade, php, webdev
Published: 2021-10-06 22:29:36

In the previous tutorials we created a fully CRUD `API` with unit tests, in this tutorial we'll understand how to work with views in `Laragine`.

When we installed Laragine in the previous tutorial, it's created under the `core` folder as we know, and also it has created a folder called the `Base`, we can't discuss everything in the `Base` folder in this tutorial, but I'll explain it separately.

under `Base` folder there are two folders called `views - unit_template`.

* In the **views** folder there's a folder called `layout`, in `layout` folder you should create the base views for your website like `navbar sidebar - buttons - etc..`
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/34wb57ou8e3zg2t2os96.png)
 
### What's `unit_template` for, in `core\Base`?
let's assume that you'll create a `blog`, the `blog` has units like `post - comment - like`, each `unit` will have `get - store - update - delete` methods, right?

So each unit will have a form for creating - update and a page for showing all units - specific unit, so in each unit, you'll create these files and implement it, instead of creating the views each time in the unit you can put the default views that will be in each unit like `form - table - show` page and when create a unit you'll just add the non-common other stuff like `attributes - titles - etc..` in the `form - table - show page - etc..` and make minor changes depends on your project and your way.

let's go to the `unit_template` folder in `core\Base` you'll see five files `_form - create - edit - index - show` you can remove the current `code` **it's just an example** and put your own `code` inside it and make a style for it. you might be thinking now how can I use these files!

##Note
Please take a look at the blade files inside `core/Base/views` and `core/Base/unit_template` you will notice that `$global` variable is shared across all the views, the `$global` variable will help you to load `views - route` dynamically.
**I'll make seperated tutorial about the `$global` variable**

I'll edit the `_form` file as an example in `core/Base/unit_template`
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x3n3ejkuo04ylb0j98p7.png)
 
* Let's create a new unit called `comment` under `blog` module
**Blog module that we created in the previous tutorial** 
`php artisan laragine:unit comment --module=blog --init`

* now I'll not discuss what we should do with `JSON` file if you don't know please see previous **tutorial number (1)**

* Let's run the second part of `unit command`
`php artisan laragine:unit comment --module=blog`

* let's go to `core\Blog\views\comment`
if you remember I have just updated the `_form` file in `core/Base/unit_template` let's see it in the `views` in `comment` unit

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgq0rhg8bx2r2fuc84g1.png)

it's the same code that I put in the `_form` file in the `core/Base/unit_template` folder, right? so we can conclude that each unit will copy the code under the `core\Base\unit_template` folder.

**What're the benefits?**
You might be wondering now what're the benefits of doing this, you don't need to write the same code each time in `default files` in each `unit` like `forms - tables - shows` page, you will just put your `template` with all `style` then the views will be created automatically for you, then you'll just write other stuff like `attributes - titles - etc..` inside them.

*I mean by saying `attributes` the name of `attributes` in your table let's say when creating a `_form` view, you'll need to create new `inputs` for each `attribute` like `title - body - etc..`

# Now let's put a route for views
 go to `core\Blog\routes\web.php` and add `        Route::resource('comments', 'CommentController')->except([
   'store', 'update', 'destroy'
]);
 `
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8zm99dqs3zwkngy5ukir.png) 
 
# Testing
* now let's test the view, you can run `php artisan serve` to create `http-server` for your project `HTTP-server will listen to port 8000`

* let's go to `http://localhost:8000/admin/comments`**main domain - route**

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/suzrze6ab7l6kmd5gwfj.png)

* The style is not our main goal in this `tutorial` I just want to teach you how to work with `views` in `Laragine`, you can navigate to any route you want for `comments`.

let's go to `http://localhost:8000/admin/comments/create`
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4atwiz7alqeip8nkla1m.png)

If you realized my template appears as I edited in the `_form` file which is just **My Form** text.  

That's all the basic usage of `Views` in `Laragine`,
have fun experimenting in `Laragine`