Learn laravel web from scratch (6)

I have been busy for a few days and have no time to write. In fact, there is no difficulty in developing web with laravel. Next we will continue to start our project.

The previous chapter talked about the login function and the operation after we start to log in.

After the login is successful, we enter the home page of the background management page and add the following list in the left navigation bar. We need to complete


The same layout of the entire webpage is divided into several modules. The header, left and bottom are templates. First, the first thing we do is to modify the user’s information. When we log in and register, we simply write the email password. But the user is far more than this information. Let’s implement them one by one. After the user logs in successfully, we store the user’s information in the session, so we can directly get the information of the currently logged in user in the session.

In the head navigation, we get the information, click on the avatar and there will be a pop-up box,


Click profile to jump to our personal information interface


In the settings, we can fill in our own information. After filling out the submission, our information will be displayed in the left column. Before jumping over, we need to get the user's name and login email and ban it, not at will Modify our login name, just like we can't modify our own ID card, how can we find it after modification (.......)

Before that, we need to explain middleware

Laravel middleware, you can use the simplest understanding, called control authority, for example, you log in to a website, you know its route is admin/home/index This is your detailed information page, but we ask you to In the case of login, access to this page is prohibited. You must log in successfully before you can have follow-up operations. Otherwise, no matter whether the route you entered is correct or wrong, we will return it to the login page. After success, you can have follow-up operations.

1. Create middleware,

Here we create an AdminLogin middleware

php artisan make:middleware AdminLogin

创建成功之后会在Http->Middleware文件下面创建一个adminloginphp文件
然后我们编辑它,我们的要求是登录成功之后才能有后续的操作,所有里面的代码很简单,我们验证下session里面有没有当前登录的用户如果有我们通过,没有我们返回再次登录就可以了
Determine whether there is a user in the session. If the user does not exist, it will be redirected directly to the login page. If it exists, the next step will be next. By the way, redirect is a redirection.
2. Register the middleware in the facade and modify it
Middleware->kernel.php is added to the most protected $routeMiddleware array
'admins' => \App\Http\Middleware\Adminlogin::class,
admins is to give the middleware a name, followed by the middleware pointing to us, that’s it, you can create multiple middleware in a project

After finishing, we need to change the route
Edit web.php routing file
Route::group is a group route 
middleware is the middleware we just registered. The prefix is ​​our prefix, namespace is the meaning of import, here are the explanations
Needless to say the middleware, the prefix is ​​for our subsequent operations. From the above we can see that we have added admin/ in front of each route... Using the prefix we can omit it in the following route definition Up. But only our prefix is ​​admin, namespace is the folder of our controller, our controller is under the Admin price, we can also omit it as long as it is added to the group routing, and we can write routing in the future. Home routing is as simple as
We have modified all the files. After we successfully log in, the home page is home.
Our route is admin/home. Now we launch this route and enter home directly in the browser to verify our success.
After exiting, we directly enter http://localhost/tourism/admin/home
He will be redirected directly to the login page, indicating that our session judgment in the middleware has taken effect. The user is empty. We cannot enter the next step. Only after entering the user name and password can the login succeed.

Okay, we can modify personal information now
We need to store the value of the session in the header template, so I stored the username in the session in the header
Find position on the head

You can directly call the user in the session to use it on the page
引入我们的头部和左侧栏这样我们整个home页面就全了就会出现home页面了,添加超链接

blade模板中图片,数据库查询出来的东西,都是一对{
  
  {}} 里面如果是图片就asset(),是链接就url(),这我们将session里面的user_id取出来传递到profile这个路由里面,在web.php里面定义路由,添加参数user_id

由于我们现在操作的还是user表前面的bolg中我们已经创建过了所以就不用创建了model 
定义了一个路由指向了LoginController控制器下面的profile方法
然后在LoginController控制器下创建本方法

通过session里面的user_id我们可以查询到数据库里面的信息其实我本人是不太喜欢这样写sql语句,比较喜欢写原生的sql但是没办法使用这个框架纪要遵守这个框架的规则
DB::table('user')->where('user_id',$user_id)->first();
这条sql语句等同于 select * from user where user_id = 1 后面的first是只去第一条,当然我们也不可能存在多条,
关于后面的数组分割截取现在不用管
完了之后我们将查询到的数据user使用compact返回到view文件夹admin.profile模板在这个里面包含了我们所有的个人信息由于可以无限制的修改个人信息,所以我们需要将查询到的数据反填充到页面当中去

完了之后我就能看到个人的信息在页面了,由于第一次进入个人中心,所以所有的信息都是空的只有我们的用户名和邮箱

我们需要达到的效果是在右侧填写完自己的信息在左侧给显示出来其实就是右侧添加,左侧查询

我们添加以上信息点击提交之后再左侧查询出来就会出现如下效果

返回到首页就会显示我们的信息,关于个人技能那就是上面对数据进行分割,截取的效果,下面是代码
在提交页面之前,我们需要在页面form表单中添加提交地址,就是我们的路由,这样我们在路由里面就有定义了一个update的路由带了一个id的参数,表单提交采用post的方式,{
  
  {csrf_firld()}}是token值,这也是laravel的好处,


在控制器里面继续写我们的update方法

这是方法在这我们用到了事物,当添加成功之后我们重定向当前页面就好了,在表单中我们使用了validateform表单验证,
大家可以去放上搜索layer这个jquery插件比较喜欢里面的表情包所以就用了,在这我们做其他的验证就是所有的项都必填,在表单中加上onsubimt = "return validateform()" 就可以刻 这样在表单提交之前就会走事件里面的方法,没填返回flase,所有的检测都通过的话返回true就可以提交成功了,好了我们这个功能就做好了

Guess you like

Origin blog.csdn.net/mzjmc123/article/details/76849882