This should be a relatively common problem. When I deal with the front-end and back-end, I need to determine whether there is a session to determine whether the user is logged in.
But the method I saw through the documentation is that I can only introduce a judgment once on each page, and then perform other operations. However, there must be a simpler way to deal with this matter. I really can’t figure it out myself. Please ask someone. help!
The main thing is how to set it up once, and then let all pages in the specified directory determine whether to log in, so as to facilitate the next operation
第一種引用訪問:將所有session判斷驗證寫在一個類裡面。在每一個控制器裡創建前置控制器方法或者初始化控制,在方法裡面直接引用session類的登錄驗證方法。
第二種繼承:就是每個控制器都繼承session驗證類,這樣每次訪問控制器都繼承session所有類與方法,在session類設置初始化控制,作為session的登錄驗證
個人推薦第二種,因為不需要在每個控制器裡面初始化控制器,減少代碼便於維護
写一个基类如
Base.php
,通过其_initialize
来实现,如:其中
Login.php
不能继承Base.php
,否则需要另外的特殊判断,如:我也是小白一枚,只是有个想法,可以在登录存储session之后,在后端写一个判断session的公共方法,然后在需要目录下面嗲用这个方法,这样就不需要在每个目录下面都去判断了!
写一个验证中间件(行为),然后在路由里调用,可以任意指定哪些页面需要调用这个验证的中间件.
可以看下手册里行为那一部分
不用每个页面引入判断啊,你把所有的控制器继承一个公共的控制器,在公共的控制器写一次session判断就可以了