“Laravel 8: Session does not apply” 可以翻译为 “Laravel 8:会话不生效”。

huangapple go评论149阅读模式
英文:

Laravel 8: Session does not apply

问题

I'm here to provide translations. Here's the translated content:

  1. 我在Laravel 8中,想要在`login.blade.php`中向用户显示错误消息,当他输入错误的凭据登录他的帐户时。
  2. 所以在控制器中,我有以下代码:
  3. if (Auth::attempt($credentials)) {
  4. // 登录用户
  5. } else {
  6. Session::flash('email', '错误的凭据');
  7. return redirect()->back();
  8. }
  9. 然后在Blade模板中:
  10. @if(\Session::has('email'))
  11. @dd(1)
  12. @endif
  13. 但是`@dd(1)` 不知何故不起作用。这意味着会话电子邮件未提交。
  14. 我还在Blade模板末尾添加了`@php dump(session()->all()); @endphp`,以查看会话,但结果是`[]`
  15. 那么,这里出了什么问题?我该如何解决这个问题?
  16. 以下是`config/session.php`的内容:
  17. <?php
  18. use Illuminate\Support\Str;
  19. return [
  20. /*
  21. |--------------------------------------------------------------------------
  22. | 默认会话驱动程序
  23. |--------------------------------------------------------------------------
  24. |
  25. | 此选项控制将在请求上使用的默认会话“驱动程序”。默认情况下,我们将使用轻量级的本地驱动程序,
  26. | 但是您可以指定这里提供的任何其他精彩驱动程序。
  27. |
  28. | 支持:“file”、“cookie”、“database”、“apc”、
  29. | “memcached”、“redis”、“dynamodb”、“array”
  30. |
  31. */
  32. 'driver' => env('SESSION_DRIVER', 'file'),
  33. /*
  34. |--------------------------------------------------------------------------
  35. | 会话生存时间
  36. |--------------------------------------------------------------------------
  37. |
  38. | 在此处,您可以指定会话在过期之前允许保持空闲的分钟数。如果您希望它们在浏览器关闭时立即过期,
  39. | 请设置该选项。
  40. |
  41. */
  42. 'lifetime' => env('SESSION_LIFETIME', 120),
  43. 'expire_on_close' => false,
  44. /*
  45. |--------------------------------------------------------------------------
  46. | 会话加密
  47. |--------------------------------------------------------------------------
  48. |
  49. | 此选项允许您轻松指定在存储之前应加密所有会话数据。
  50. | 所有加密将由Laravel自动运行,您可以像正常使用会话一样使用它。
  51. |
  52. */
  53. 'encrypt' => false,
  54. /*
  55. |--------------------------------------------------------------------------
  56. | 会话文件位置
  57. |--------------------------------------------------------------------------
  58. |
  59. | 当使用本机会话驱动程序时,我们需要一个位置来存储会话文件。
  60. | 默认为您设置了一个,但可以指定不同的位置。这仅适用于文件会话。
  61. |
  62. */
  63. 'files' => storage_path('framework/sessions'),
  64. /*
  65. |--------------------------------------------------------------------------
  66. | 会话数据库连接
  67. |--------------------------------------------------------------------------
  68. |
  69. | 当使用“数据库”或“redis”会话驱动程序时,您可以指定应该用于管理这些会话的连接。
  70. | 这应该与数据库配置选项中的连接对应。
  71. |
  72. */
  73. 'connection' => env('SESSION_CONNECTION', null),
  74. /*
  75. |--------------------------------------------------------------------------
  76. | 会话数据库表
  77. |--------------------------------------------------------------------------
  78. |
  79. | 当使用“数据库”会话驱动程序时,您可以指定用于管理会话的表。
  80. | 当然,为您提供了明智的默认值;但是,您可以根据需要自由更改。
  81. |
  82. */
  83. 'table' => 'sessions',
  84. /*
  85. |--------------------------------------------------------------------------
  86. | 会话缓存存储
  87. |--------------------------------------------------------------------------
  88. |
  89. | 在使用框架的缓存驱动会话后端之一时,您可以列出应该用于这些会话的缓存存储。
  90. | 此值必须与应用程序配置的缓存“存储”之一匹配。
  91. |
  92. | 影响:“apc”、“dynamodb”、“memcached”、“redis”
  93. |
  94. */
  95. 'store' => env('SESSION_STORE', null),
  96. /*
  97. |--------------------------------------------------------------------------
  98. | 会话清理抽奖
  99. |--------------------------------------------------------------------------
  100. |
  101. | 一些会话驱动程序必须手动扫描其存储位置以从存储中删除旧会话。
  102. | 在给定的请求上发生的机会如下。默认情况下,几率是100中的2。
  103. |
  104. */
  105. 'lottery' => [2, 100],
  106. /*
  107. |--------------------------------------------------------------------------
  108. | 会话Cookie名称
  109. |--------------------------------------------------------------------------
  110. |
  111. | 在此处,您可以更改用于通过ID标识会话实例的cookie的名称。
  112. | 每次由框架为每个驱动程序创建新的会话cookie时,都将使用指定的名称。
  113. |
  114. */
  115. 'cookie' => env(
  116. 'SESSION_COOKIE',
  117. Str::slug(env('APP_NAME', 'laravel'), '_').'_session'
  118. ),
  119. /*
  120. |--------------------------------------------------------------------------
  121. | 会话Cookie路径
  122. |--------------------------------------------------------------------------
  123. |
  124. | 会话cookie路径确定cookie将被视为可用的路径。
  125. | 通常,这将是您的应用程序的根路径,但在必要时可以自由更改。
  126. |
  127. */
  128. 'path' => '/',
  129. /*
  130. |--------------------------------------------------------------------------
  131. | 会话Cookie域
  132. |--------------------------------------------------------------------------
  133. |
  134. | 在此处,您可以更改应用程序中用于标识会话的cookie的域。
  135. | 这将确定哪些域可以在应用程序中使用该cookie。已设置了明智的默认值。
  136. |
  137. */
  138. 'domain' => env('SESSION_DOMAIN', null),
  139. /*
  140. |--------------------------------------------------------------------------
  141. | 仅限HTTPS的Cookie
  142. |--------------------------------------------------------------------------
  143. |
  144. | 将此选项设置为true,会话cookie只会在浏览器具有HTTPS连接时发送回服务器。
  145. | 这将防止在无法安全发送时将cookie发送给您。
  146. |
  147. */
  148. 'secure' => env('SESSION_SECURE_COOKIE'),
  149. /*
  150. |--------------------------------------------------------------------------
  151. | 仅HTTP访问
  152. |--------------------------------------------------------------------------
  153. |
  154. | 将此值设置为true将阻止JavaScript访问cookie的值,
  155. | 并且只能通过HTTP协议访问cookie。如有需要,您可以自由修改此选项。
  156. |
  157. */
  158. 'http_only' => true,
  159. /*
  160. |--------------------------------------------------------------------------
  161. | Same-Site Cookies
  162. <details>
  163. <summary>英文:</summary>
  164. I&#39;m in Laravel 8 and I wanted to show an error message to user when he enters wrong credentials for signing into his account at `login.blade.php`.
  165. So in the Controller, I have this:
  166. if(Auth::attempt($credentials)){
  167. // log user in
  168. }else{
  169. Session::flash(&#39;email&#39;, &#39;wrong credentials&#39;);
  170. return redirect()-&gt;back();
  171. }
  172. Then in the Blade:
  173. @if(\Session::has(&#39;email&#39;))
  174. @dd(1)
  175. @endif
  176. But `dd(1)` does not work out somehow. Meaning that the session email was not submitted.
  177. I also added the `@php dump(session()-&gt;all()); @endphp` at the end of blade to see the sessions but it is `[]`
  178. So what&#39;s going wrong here? How can I solve this issue?
  179. Here is the `config/session.php`:
  180. &lt;?php
  181. use Illuminate\Support\Str;
  182. return [
  183. /*
  184. |--------------------------------------------------------------------------
  185. | Default Session Driver
  186. |--------------------------------------------------------------------------
  187. |
  188. | This option controls the default session &quot;driver&quot; that will be used on
  189. | requests. By default, we will use the lightweight native driver but
  190. | you may specify any of the other wonderful drivers provided here.
  191. |
  192. | Supported: &quot;file&quot;, &quot;cookie&quot;, &quot;database&quot;, &quot;apc&quot;,
  193. | &quot;memcached&quot;, &quot;redis&quot;, &quot;dynamodb&quot;, &quot;array&quot;
  194. |
  195. */
  196. &#39;driver&#39; =&gt; env(&#39;SESSION_DRIVER&#39;, &#39;file&#39;),
  197. /*
  198. |--------------------------------------------------------------------------
  199. | Session Lifetime
  200. |--------------------------------------------------------------------------
  201. |
  202. | Here you may specify the number of minutes that you wish the session
  203. | to be allowed to remain idle before it expires. If you want them
  204. | to immediately expire on the browser closing, set that option.
  205. |
  206. */
  207. &#39;lifetime&#39; =&gt; env(&#39;SESSION_LIFETIME&#39;, 120),
  208. &#39;expire_on_close&#39; =&gt; false,
  209. /*
  210. |--------------------------------------------------------------------------
  211. | Session Encryption
  212. |--------------------------------------------------------------------------
  213. |
  214. | This option allows you to easily specify that all of your session data
  215. | should be encrypted before it is stored. All encryption will be run
  216. | automatically by Laravel and you can use the Session like normal.
  217. |
  218. */
  219. &#39;encrypt&#39; =&gt; false,
  220. /*
  221. |--------------------------------------------------------------------------
  222. | Session File Location
  223. |--------------------------------------------------------------------------
  224. |
  225. | When using the native session driver, we need a location where session
  226. | files may be stored. A default has been set for you but a different
  227. | location may be specified. This is only needed for file sessions.
  228. |
  229. */
  230. &#39;files&#39; =&gt; storage_path(&#39;framework/sessions&#39;),
  231. /*
  232. |--------------------------------------------------------------------------
  233. | Session Database Connection
  234. |--------------------------------------------------------------------------
  235. |
  236. | When using the &quot;database&quot; or &quot;redis&quot; session drivers, you may specify a
  237. | connection that should be used to manage these sessions. This should
  238. | correspond to a connection in your database configuration options.
  239. |
  240. */
  241. &#39;connection&#39; =&gt; env(&#39;SESSION_CONNECTION&#39;, null),
  242. /*
  243. |--------------------------------------------------------------------------
  244. | Session Database Table
  245. |--------------------------------------------------------------------------
  246. |
  247. | When using the &quot;database&quot; session driver, you may specify the table we
  248. | should use to manage the sessions. Of course, a sensible default is
  249. | provided for you; however, you are free to change this as needed.
  250. |
  251. */
  252. &#39;table&#39; =&gt; &#39;sessions&#39;,
  253. /*
  254. |--------------------------------------------------------------------------
  255. | Session Cache Store
  256. |--------------------------------------------------------------------------
  257. |
  258. | While using one of the framework&#39;s cache driven session backends you may
  259. | list a cache store that should be used for these sessions. This value
  260. | must match with one of the application&#39;s configured cache &quot;stores&quot;.
  261. |
  262. | Affects: &quot;apc&quot;, &quot;dynamodb&quot;, &quot;memcached&quot;, &quot;redis&quot;
  263. |
  264. */
  265. &#39;store&#39; =&gt; env(&#39;SESSION_STORE&#39;, null),
  266. /*
  267. |--------------------------------------------------------------------------
  268. | Session Sweeping Lottery
  269. |--------------------------------------------------------------------------
  270. |
  271. | Some session drivers must manually sweep their storage location to get
  272. | rid of old sessions from storage. Here are the chances that it will
  273. | happen on a given request. By default, the odds are 2 out of 100.
  274. |
  275. */
  276. &#39;lottery&#39; =&gt; [2, 100],
  277. /*
  278. |--------------------------------------------------------------------------
  279. | Session Cookie Name
  280. |--------------------------------------------------------------------------
  281. |
  282. | Here you may change the name of the cookie used to identify a session
  283. | instance by ID. The name specified here will get used every time a
  284. | new session cookie is created by the framework for every driver.
  285. |
  286. */
  287. &#39;cookie&#39; =&gt; env(
  288. &#39;SESSION_COOKIE&#39;,
  289. Str::slug(env(&#39;APP_NAME&#39;, &#39;laravel&#39;), &#39;_&#39;).&#39;_session&#39;
  290. ),
  291. /*
  292. |--------------------------------------------------------------------------
  293. | Session Cookie Path
  294. |--------------------------------------------------------------------------
  295. |
  296. | The session cookie path determines the path for which the cookie will
  297. | be regarded as available. Typically, this will be the root path of
  298. | your application but you are free to change this when necessary.
  299. |
  300. */
  301. &#39;path&#39; =&gt; &#39;/&#39;,
  302. /*
  303. |--------------------------------------------------------------------------
  304. | Session Cookie Domain
  305. |--------------------------------------------------------------------------
  306. |
  307. | Here you may change the domain of the cookie used to identify a session
  308. | in your application. This will determine which domains the cookie is
  309. | available to in your application. A sensible default has been set.
  310. |
  311. */
  312. &#39;domain&#39; =&gt; env(&#39;SESSION_DOMAIN&#39;, null),
  313. /*
  314. |--------------------------------------------------------------------------
  315. | HTTPS Only Cookies
  316. |--------------------------------------------------------------------------
  317. |
  318. | By setting this option to true, session cookies will only be sent back
  319. | to the server if the browser has a HTTPS connection. This will keep
  320. | the cookie from being sent to you when it can&#39;t be done securely.
  321. |
  322. */
  323. &#39;secure&#39; =&gt; env(&#39;SESSION_SECURE_COOKIE&#39;),
  324. /*
  325. |--------------------------------------------------------------------------
  326. | HTTP Access Only
  327. |--------------------------------------------------------------------------
  328. |
  329. | Setting this value to true will prevent JavaScript from accessing the
  330. | value of the cookie and the cookie will only be accessible through
  331. | the HTTP protocol. You are free to modify this option if needed.
  332. |
  333. */
  334. &#39;http_only&#39; =&gt; true,
  335. /*
  336. |--------------------------------------------------------------------------
  337. | Same-Site Cookies
  338. |--------------------------------------------------------------------------
  339. |
  340. | This option determines how your cookies behave when cross-site requests
  341. | take place, and can be used to mitigate CSRF attacks. By default, we
  342. | will set this value to &quot;lax&quot; since this is a secure default value.
  343. |
  344. | Supported: &quot;lax&quot;, &quot;strict&quot;, &quot;none&quot;, null
  345. |
  346. */
  347. &#39;same_site&#39; =&gt; &#39;lax&#39;,
  348. ];
  349. </details>
  350. # 答案1
  351. **得分**: 1
  352. 前往 `App/Http/Kernel.php` 文件。
  353. `\Illuminate\Session\Middleware\StartSession::class` 这行代码添加到 `$middleware` 数组中。请查看下面的截图。
  354. ![截图][1]
  355. [1]: https://i.stack.imgur.com/Vk1to.png
  356. 就是这样。现在我们来测试一下会话功能。
  357. ****
  358. 如果仍然存在问题,请尝试以下方法来存储数据。希望对你有所帮助。
  359. 尝试使用全局的 `session()` 辅助函数,方法如下。
  360. **-- 存储数组**
  361. ```php
  362. Session::push('key', $array);
  363. //或者
  364. session()->push('key', $array);

-- 按键存储会话

  1. Session::put('session_key', 'value');
  2. //或者
  3. session(['key' => 'value']);

-- 通过键检索会话

  1. Session::get('key');
  2. //或者
  3. session('key');

-- 从会话获取所有数据

  1. Session::all();
  2. //或者
  3. session()->all();
英文:
  1. Go to the App/Http/Kernel.php file.
  2. Add this line \Illuminate\Session\Middleware\StartSession::class, into the $middleware array. Please check the below screenshot.

“Laravel 8: Session does not apply” 可以翻译为 “Laravel 8:会话不生效”。

That’s it. Let’s test the session now.


If you still have an issue then try the following way to store the data.
I hope that helps.

Try to use a global session() helper function as below.

-- To Store An Array

  1. Session::push(&#39;key&#39;, $array);
  2. //or
  3. session()-&gt;push(&#39;key&#39;, $array);

-- Store Session By a Key

  1. Session::put(&#39;session_key&#39;, &#39;value&#39;);
  2. //or
  3. session([&#39;key&#39;=&gt;&#39;value&#39;]);

-- Retrieving Session By Key

  1. Session::get(&#39;key&#39;);
  2. //or
  3. session(&#39;key&#39;);

-- Get All Data From The Session

  1. Session::all();
  2. //or
  3. session()-&gt;all();

huangapple
  • 本文由 发表于 2023年5月7日 13:38:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192350.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定