We find the ticket using its slug, then we use the tickets. This view is very similar to the create view, but we add a new checkbox to modify tickets status. If the status is 1 pending , we display nothing, the checkbox is not checked. If the status is 0 answered , we display a checked attribute, the checkbox is checked. We use the action helper again! When you click on the edit button, you should be able to access the edit form:.
Unfortunately, we cant update the ticket yet. Remember what youve done to create a new ticket? We need to use POST method to submit the form. Then use update action to handle the submission and store the changes. Delete a ticket Youve learned how to create, read and update a ticket.
Next, you will learn how to delete it. By the end of this section, youll have a nice CRUD application! First step, lets open the routes. We find the ticket using the provided slug. As always, we then redirect users to the all tickets page. Lets update the index. Finally, in order to remove the ticket, all we need to do is create a form to submit a delete request.
Open show. The code above creates a nice delete form for you. Now, click the delete button, you should be able to remove the ticket! Youve just deleted a ticket! Sending an email When users submit a ticket, we may want to receive an email to get notified.
In this section, you will learn how to send emails using Laravel. Laravel provides many methods to send emails. To send emails on a production server, simply edit the mail. Sending emails using Gmail If you have a gmail account, its very easy to send emails using Laravel 5! You must turn the option Allow less secure apps ON. Youre now ready to send emails using Gmail! If you get this error when sending email: Failed to authenticate on SMTP server with username youremail gmail.
Sending a test email To send a test email, open routes. The name of the view that we use to send emails. An array of data that we want to pass to the email.
A closure that we can use to customize our email subjects, sender, recipients, etc. If the email is sent successfully, Laravel will display a message. Finally, we dont have the welcome. Check your inbox, you should receive a new email!
Feel free to customize your email address, recipients, subjects, etc. Sending an email when there is a new ticket Now that we have set up everything, lets send an email when users create a new ticket! Open TicketsController. Mail::send 'emails. Its time to create a new ticket! If everything works out well, you should see a new email in your inbox! Reply to a ticket Welcome to the last section! In this section, we will learn how to create a form for users to post a reply.
Create a new comments table First, we need a table to store all the ticket responses. I name this table comments. Lets run this migration command:. You should know how to read this file by now. Lets run the migrate command to create the comments table and its columns:. Check your database now to make sure that you have created the comments table. Introducing Eloquent: Relationships In Laravel, you can maintain a relationship between tables easily using Eloquent.
Here are the relationships that Eloquent supports:. What is a relationship? Usually, tables are related to each other. For instance, our tickets may have many comments ticket responses. That is One to Many relationship. Once weve defined a One to Many relationship between tickets and comments table, we can easily access and list all comments or any related records. Create a new Comment model As you know, we need a Comment model!
Run this command to create it:. By doing this, we let Eloquent know that this Comment model belongs to the Ticket model. Thats it! Youve defined a One to Many relationship between two tables. Create a new comments controller With the relation defined, we will create a new CommentsController to handle form submissions and save comments to our database.
Before doing that, lets modify our routes. Its time to run this command to generate our controller:. As you see, we still use Request here for the validation. Create a new reply form Good job!
Now open the tickets. When you have the form, lets try to reply to a ticket. Display the comments One last step, were going to modify the show action of our TicketsController to list all tickets comments and pass them to the view. Amazing, right? You dont even use a single SQL code! Now, open the show. Refresh your browser now! To make sure that everything is working, reply again!
Chapter 3 Summary In this chapter, youve gone through the different steps involved in creating a ticket support system. Even though the app is simple, it provides us many things to learn:. Youve known how to create databases.
Youve learned one of the most important Laravel features: migrations. Now you can create any database structures that you want. Youve understood how to use Request to validate forms. If you want to use different packages, youve known how to install them. Youve learned about Laravels helper functions. Sending emails using Gmail and Sendgrid is easy, right? Youve known how to define Eloquent Relationships and work with those relationships easily.
Basically, you may now be able to create a simple blog system. Feel free to build a different application or customize this application to meet your needs. The next chapter is where all the fun begin! You will learn to create a complete blog application that has an admin control panel. You may use this application to write your blog posts or you may use it as a starter template for all your amazing applications.
Chapter 4: Building A Blog Application Up to this point, we have used many Laravel features to build our applications. In this chapter, were going to build a blog application. By doing this, we will learn about Laravel Authentication, Seeding, Localization, Middleware and many useful features that can help us to have a solid understanding about Laravel 5. For our purposes, its always best to think about how our blog application works first.
I assume that you have followed the instructions provided in the previous chapter and youve created a support ticket system. We will use the previous application as our template. Users can be able to register and login. Admin can write posts. Users can be able to comment on the posts. There is an admin control panel to manage users, posts create, update, delete Permissions and roles system.
Building a user registration page Since Laravel 5, implementing authentication has become very easy, Laravel has provided almost everything for us.
In this section, you will learn how to create a simple registration page. First, we need to create some routes. The first route will provide the registration form.
The second route will process the form. Both routes are handled by the AuthControllers actions: getRegister and postRegister. By default, Laravel has created the AuthController for us. You can find it in the Auth directory. Lets open the file and take a look at:. As you may notice, there are three fields here: name, email and password.
Unfortunately, Laravel doesnt create the registration view for us, we have to create it manually. Here is the code:. Youve created many forms in the previous chapters, so I guess you should understand this file clearly.
Instead of using this line to generate a new CSRF token:. You also may notice that there is a new old method. When the forms validation fails, the users will be redirected back to the form.
We use this method to display the old users input, so they dont have to fill in all the fields again. Chapter 4: Building A Blog Application Fill in all the fields, and hit submit! Youve registered a new user! You can fix this error by adding the home route into your app or you may simply ignore it. Youll learn how to redirect users to other locations in the next section.
Creating a logout functionality When users register for a new account, Laravel will validate the registration form. To redirect users to other locations, open AuthController.
You may notice that when you go to the registration page, Laravel will automatically redirect you back to the home page because youre now logged in. We dont have the logout functionality yet, but dont worry, its very easy to implement.
To check whether a user is logged in, we can use the Auth::check method. In the code above, if users are logged in, we will display a logout link. If youre using Laravel 5. Try to test the functionality yourself! Now you can be able to log out! Creating a login page Its time to create our login form.
As you may have guessed, you should create a login view now. Our login form is simple, it has two fields: email and password. Users have to enter the correct email and password here to login.
Laravel also provides the remember me functionality out of the box. We can implement it by simply creating a remember checkbox. Well done. Add authentication throttling to your application Laravel 5. This feature is used to throttle login attempts to your application. If users try to login many times, they cant be able to login for one minute. You may skip these steps. By the way, Its still good to know how it works. As were using the AuthController class for authentication, you can easily integrate this feature by doing the following steps: First, be sure to update your application to version 5.
You can update your application by vagrant ssh to your VM, then cd to your Laravel root directory, and run:. Once updated, open AuthController. Youve implemented the throttling feature. Now, lets try to login with wrong credentials;. Building an admin area Imagine that our application will have an administration section and a front end section, there would be many routes.
We need to find a way to organize all the routes. Additionally, we may want to allow only administrators to access our admin area.
Fortunately, Laravel helps us to do that easily. Lets open routes. By using Route::group we can group all related routes together and apply some specific rules for them. We use the prefix attribute to prefix each route in the route group with a URI admin.
Laravel 5. Your applications controllers, models and other classes must be namespaced. What are namespaces? According to PHP docs: namespaces are designed to solve two problems that authors of libraries and applications encounter when creating re-usable code elements such as classes or functions..
Simply put, lets think namespaces as the last names of persons. When many persons have the same name, we will use their last name to distinguish them apart. To namespace a class, we can use the namespace keyword and declare the namespace at the top of the file before any other code. For instance, lets open AuthController. Laravel will know exactly which class that we want to load and where to find it. Basically, we use it to filter our applications HTTP requests.
That means I want to use the auth middeware for this route group. Only authenticated users can access these routes. Well learn more about Middleware soon. List all users We now have a route group for our admin control panel. Lets list all the users so that we can view and manage them easier. As you know, we have defined a route here:. This time, our code is a little bit different. Laravel is clever. When we code like this, it will automatically create a directory called Admin, and put the UsersController file into the Admin directory for you.
More than that, our controller has been namespaced! At this point, I think that youve known how to list all the users. Basically, the process is very similar to what weve done to list all the tickets in Chapter 3. As you may have guessed, we will put all the administration views in the backend directory. Were going to create a new view called index. Lets create a new users directory as well and put the index view inside.
We can be able to view all the users! Dont worry, its because our Middleware are working fine. We will learn how to use Middleware and fix this bug in the next section. Imagine that you have a layer between all requests and responses. We call the layer: Middleware. We can use middleware to do many things. For example, middleware can help to authenticate users, log data for analytics, add CSRF protection, etc.
Open the directory, youll see four middleware:. Authenticated middleware: This middleware is responsible for authenticating users. If users are not logged in, they are redirected to the login page. EncryptCookies middleware: Used to encrypt the applications cookies. RedirectIfAuthenticated middleware: Redirect users to a page if theyre not authenticated.
As you see, this Authenticated middleware is just a class. We use the handle method to process all requests and define request filters. To fix the bug, we can just modify the line to:. Everything should be good to go. Now lets say that we wanted to use a new middleware called Manager to make sure that only administrators can access the admin area.
Creating a new middleware Creating a new middleware is easy, simply run this Artisan command:. One more step to do, we need to register the Manager middleware. Lets open the Kernel. However, we just want to assign the Manager middleware to our admin route group. Youve got a solid foundation of Middleware. Keep in mind that you can use Middleware to do many things. Even though our Manager middleware is working properly, we cant see any difference. The reason is, we dont create any request filters yet.
If we want to restrict access to the admin area, we need to add roles or permissons to our users. Fortunately, Laravel has a very popular package that can help us to implement the feature easily: Entrust. Adding roles and permission to our app using Entrust Many Laravel developers are using Entrust to add Role-based Permissions to their Laravel applica- tions.
Installing Entrust package for Laravel 5. If you use Laravel 5. The official Entrust package now supports Laravel 5. Next, run composer update to install Entrust. If you are going to use Middleware requires Laravel 5. You may use this file to customize table names and model namespaces. Entrust need some database tables to work. Run this command to generate the migrations:.
Next, we need to create two models: Role and Permission. Create a new file called Role. This is Entrust trait. You should have something like this:. If you use an older version of Laravel or youve installed Entrust, please skip this section. The official Entrust package doesnt support Laravel 5. We have to install a different branch of Entrust to make it work properly. When the official Entrust package is updated, you may use the official version check the next section.
In order to install Entrust for Laravel 5. Create Entrust roles Once installed, we can be able to create user roles. Lets say we will have two roles: manager and member.
As an exercise lets create a simple role creation form: First, edit the routes. We define routes to view all roles and create a new role. Again, create RolesController by running this command:.
Then create a new view called create:. Next, lets update the store action of the RolesController to save the data. We use RoleFormRequest here to validate the form, but we dont have the request file yet. Lets create it:. The Display Name and Description fields are optional. We only need to require users to enter the roles name. You should see a list of roles. Assign roles to users In this section, we will learn how to edit users and assign roles to them.
Lets start by adding these routes to our admin route group:. All we need to do is find a correct user using the user id and list all the roles for users to select. As you see, we use the Role model here. Dont forget to add this line at the top of the UsersController:. This will display a multiple select box for us. Click on the name of a user that you want to edit.
If you click the Submit button now, nothing will happen. Notice how we handle the password:. First, we check if the password is empty. We only save the password when users enter a new one, using:. This will create a hashed password.
Pernah juga beli hardcopy, tapi lebih practical dalam softcopy. Senang nak share dan copy source code. Selepas bayar terus bleh download, kalau hardcopy mungkin ambil masa untuk courier ke Malaysia pulak. Buku ni aku beli semasa 1st Edition Laravel 5.
Pembangunan dua applikasi ini cukup untuk membawa pembaca mengenali, memahami, dan menggunakan Framework Laravel dari ground up. Installasi server, installasi laravel framework, installasi vendor, konfigurasi model, controller, view, eloquent, relationship etc.
Secara umumnya, buku ini amat sesuai dibaca dan dijadikan rujukan oleh pengguna baru dan lama. Our mission is to help people learning Laravel and building awesome websites.
Don't forget to join our Facebook, Twitter, Instagram fan pages and subscribe to our mailing list. If you have any questions, feel free to contact us at [email protected].
Learning Laravel Book, Edition. Building Applications With Bootstrap 4. There are many proven code rich recipes for working with Laravel. Think about this book as a collection of all premium Laravel tutorials or the successor to the popular Learning Laravel 5 book.
Laravel 5 Cookbook also includes tested code that you can download and reuse in your own applications. We also have a forum for discussion and debate. You can freely ask any questions, provide your valuable feedback and help others. The projects in this book are intended to help people who have grasped the basics of PHP and HTML to move forward, developing more complex projects, using Laravel advanced techniques. The fundamentals of the PHP and Laravel are not covered, you will need to:.
Save my name, email, and website in this browser for the next time I comment.
0コメント