top of page
Search
  • Nov 5, 2022
  • 3 min read

Updated: Dec 13, 2022

View

Laravel has its own structure. Model, View and Controller (MVC) are three main pillar of Laravel, View provide a way to place all HTML in separate files and distinct between application logic and presentation logic. Views are live in the resources/views directory.


Blade

Blade is a Laravel powerful templating engine. Its file uses the .blade.php file extension and is typically stored in the resources/views directory. I have already demonstrated the home.blade.php and admin-home.blade.php in previous post. In here, I am going to explore create.blade.php, edit.blade.php and delete.blade.php.


@csrf

Laravel automatically generates a CSRF token for each active user session, the aim of CSRF token is to make sure authenticated users make the request to the application. To prevent malicious attack @csrf token will be used in every POST, PUT, PATCH and DELETE request. I have used @csrf blade directive in Create.blade.php, edit.blade.php and delete.blade.php to generate hidden token input field and validate the request.


@error

@error blade directive has been used for validation error message. within an @error directive, you display the error message.


resources/views/create.blade.php

- I have previously demonstrated the template here. I am going to focus on functionality of create page.

<a href="{{ route('user.index') }}" class="btn btn-success btn-sm" title="Back to Your

Contact List"> <i class="fa fa-arrow-left"></i> Go Back </a>

I have used named route to generate url for Go Back button.


@if ($errors->any()) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif


This is the most common way to display all validation error. It will echo all the validation error messages in create view file if there any validation error exists in the $errors variable. the any() method on $errors variable is about to check there are any validation errors exist in it or not. It returns 1 if any errors exist in $errors variable. After that you can call foreach method on $errors->all() and display message using $error variable.


@if(session()->has('success')) <div class="alert alert-success alert-dismissible fade show w-50"> <button type="button" class="btn-close" data-bs-dismiss="alert"></button> {{ session()->get('success') }} </div> @endif

Display success message.


<form action="{{ route('user.store') }}" method="post" enctype="multipart/form-data"> @csrf <label> Name</label></br> <input type="text" name="name" id="name" class="form-control" value="{{

old('name') }}"></br> <label> Mobile</label></br> <input type="text" name="mobile" id="mobile" class="form-control" value="{{

old('mobile') }}"></br> <input class="form-control" name="photo" type="file" id="photo"></br> <input type="submit" value="Create" class="btn btn-success" title="Create

new Contact"></br> </form>

- Named route has been used to generate url.

- Method is posted so I used @csrf

- As the form includes input type file, I used multipart/form-data.

- Laravel has a useful feature called old input. It keeps input from one request during

the next request, this way if forms detect validation errors and you do not need to re-

populate the form. I have used old helper which is compatible with blade template to keep

input from one request during the other request.


resources/views/edit.blade.php

- I have previously demonstrated the template here, I am going to focus on functionality of edit page


<a href="{{ route('user.index') }}" class="btn btn-success btn-sm" title="Back to Your

Contact List"> <i class="fa fa-arrow-left"></i> Go Back </a>

I have used named route to generate url for Go Back button


@if ($errors->any()) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif


Within @if blade directive, I have use @foreach to iterate validation error message and display them.


@if(session()->has('success')) <div class="alert alert-success alert-dismissible fade show w-50"> <button type="button" class="btn-close" data-bs-dismiss="alert"></button> {{ session()->get('success') }} </div> @endif

Display success message.


<form method="POST" action="{{route('user.update', $contacts->id)}}"

enctype="multipart/form-data"> @csrf @method('PUT') <input type="hidden" name="id" id="id" value="{{$contacts->id}}" id="id" /> <label>Name</label></br> <input type="text" name="name" id="name" value="{{$contacts->name}}"

class="form-control"></br> <label>Mobile</label></br> <input type="text" name="mobile" id="mobile" value="{{$contacts->mobile}}"

class="form-control"></br> <input class="form-control" name="photo" type="file" id="photo"> <img src="{{ asset($contacts->photo) }}" width= '50' height='50' class="img

img-responsive" /> <input type="submit" value="Update" class="btn btn-success" title="Update

Contact"></br> </form>


- Named route has been used to generate url.

- Method is posted so I used @csrf

- As the form includes input type file, I used multipart/form-data.

- Retrieved field value from database.

- For photo I have used asset helper as it creates url.


resources/views/delete.blade.php

- I have previously demonstrated the template here, I am going to focus on functionality of delete page.

- The form method is posted so I used @csrf

- HTML forms cannot make Delete request so I used the blade @method directive to make delete request.

- I have used <a> tag and href attribute to redirect into user.index


<form action="{{ route('user.destroy', $contacts->id) }}" method="post"> @csrf @method('DELETE') <div class="form-group row mb-0" > <div class="col-md-12" style="text-align: center;"> <h4> Are you sure you want to delete {{ $contacts->name }}? </h4> </div> </div> </br> <div class="form-group row mb-0" > <div class="col-md-8 offset-md-5 "> <button type="submit" class="btn btn-danger"> Yes </button> <a href="{{ route('user.index') }}" class="btn btn-info">No</a> </div> </div> <br> <br> </form>

 
 
 
  • Nov 4, 2022
  • 1 min read

Updated: Dec 12, 2022

GitHub: It is an online software development platform and the main use of GitHub is storing, tracking and collaborating on projects. I have used GitHub in this project for backup purpose.


Git: It is an open-source version control software and normally it is used for managing and tracking file revisions. Git is one of the requirements for getting backup of Laravel project from vs code into GitHub.


Firstly, I needed to create new repository in GitHub. For this purpose, I have sign in GitHub and from repositories Press New green button.

ree

- Named the repository.

- Added a README file - optional

- Added a description - it is also optional .

- Added .gitignore - select Laravel.

- Press the create repository button.


ree

After creating repository, GitHub itself provides guidance for storing Laravel project from vs code to GitHub. I have typed the above command in terminal, for me, it worked successfully, but, sometimes, from first time user, it may ask for email and name.


git config --global user.email "you@example.com"

git config --global user.name "Your Name"


Perhaps, if by any reason, it failed to push through terminal, then in that case press ctrl+shift+p

ree

- Write Git: Add remote

- https://github.com/ATFaiz/LaravelProject.git - copy this from GitHub and paste it on command palette. then add branch.

ree

- Then add commit message and press commit button



 
 
 
  • Nov 3, 2022
  • 2 min read

Updated: Dec 12, 2022

Web Deployment: It is the process of deploying metadata or content from development environment to production environment (hosting platform). The deployment can be done manually or automatically. Deployment has different approaches with regards to metadata and content, metadata include code, templates, stylesheets, files etc., so deployment of metadata requires a validation and consistency check. While the content include text, images, videos etc., they are less complicated; therefore, deployment of content handled differently to pushing to a live environment.


Duck DNS: It is a free dynamic DNS service that provides public subdomain under ducks.org. By getting subdomain and using provided token anyone can update their records. Once everything is set, through https post every 5 minutes duck DNS has been update with latest external IP, so in this way it keeps the client record update.


Certbot: It is an open-source software for installing free TLS/SSL certificates on websites to enable HTTPS. It provides secure connection between site visitor and web server. For deployment, I have used duck dns subdomain with installed Cerbot on website to enable https.


To deploy the Laravel project, I have moved the project folder into document root directory. At first, I used FileZila ftp application but it took only an hour to download the project and it need another 1 or 2 hours to upload the Laravel Project into root directory.


ree

Therefore, I changed my mind and, because the project was already developed in AWS cloud platform, just by using Linux copy command, I copied the Laravel project to root directory.

cp -r contactapp /var/www/html

ree

Then I changed the ownership of html, contactapp, and public directories and changed the permission of storage directory. On Ubuntu, Apache runs as user www-data, so changing the directories ownership to www-data will allow me to edit web resources published by Apache.

Also, I needed to change the document root. To do this, I have been to etc/apache2/sites-available and from there I have edited document root inside 000-default-le-ssl.conf file.

DocumentRoot /var/ww/htmal/contactapp/public

ree

After Deployment - Broken Image

Despite the contactapp project working at a good standard in Laravel built in server, after deployment into hosting platform, images were broken and not clearly visible.

To resolve this issue, I deleted storage sub directory from public folder, then in routes/web.php I created new route.


Route::get('/link', function(){

Artisan::call('storage:link');

});


Then browse the following link: https:// ss-systems.duckdns.org/link

This way the broken image issue was resolved.


File Access from local storage

The other issue with images was that they were accessible with direct URL because the storage directory is inside the public folder. Based on my understand from research, the only secure way is to change the image directory path and move it outside public folder. However, through .htaccess file I have restricted access to image direct URL, although it is not very secure.

ree

 
 
 

Thanks for submitting!

© 2023 by DO IT YOURSELF. Proudly created with Wix.com

bottom of page