Laravel Blade模板运行报Class 'Session' not found错误求助
问题说明
重新克隆Laravel项目代码后,Blade模板运行抛出Class "Session" not found错误,相同代码在克隆前可正常运行,报错涉及两处模板:
- 全局消息提示组件
alert.blade.php - 仪表盘商品列表页模板
alert.blade.php原代码:
@if ($errors->any()) <div> <ul> @foreach ($errors->all() as $error) <li style="color: red">{{ $error }}</li> @endforeach </ul> </div> @endif @if (Session::has('error')) <div> <ul> <li style="color: red">{{ Session::get('error') }}</li> </ul> </div> @endif @if (Session::has('success')) <div> <ul> <li style="color: green">{{ Session::get('success') }}</li> </ul> </div> @endif
仪表盘商品列表模板原代码:
@foreach ($products as $product) <a href="/products/view/{{ $product->id }}" class="m-4"> <div class="box-border h-{{ $h_box }} w-{{ $w_box }} p-4 border-4 justify-center rounded-md"> <div class="box-border h-{{ $image_size }} w-{{ $image_size }} border-1 mx-auto"> <img class="object-cover w-full h-full" src="{{ $product->thumb }}"> </div> <div class="box-border h-16 w-{{ $image_size }} border-1 truncate mx-auto"> <h2 class="mt-1 font-medium leading-tight text-base mb-2">{{ /Str::title($product->name) }} </h2> @if ($product->price > $product->sale_price) <div class="inline-block"> <h2 class="font-medium leading-tight text-base mt-0 mb-2 text-red-500"> <p class="line-through inline">{{ /Str::title($product->price) }}$</p> -> <p class="inline">{{ /Str::title($product->sale_price) }}$</p> </h2> </div> @else <h2 class="font-medium leading-tight text-base mt-0 mb-2 text-red-500"> {{ /Str::title($product->price) }}$ </h2> @endif </div> <div class="box-border h-16 w-{{ $image_size }} border-1 truncate mx-auto"> <h2 class="mt-1 text-left leading-tight text-base mb-2">0 star | 0 sale </h2> </div> </div> </a> @endforeach
报错原因
- 新克隆项目未完成初始化:缺少composer依赖、未生成应用密钥、残留异常配置缓存,都会导致Session门面无法被框架正确识别加载。
- 模板类调用存在语法错误:仪表盘模板中
Str前错误使用正斜杠/作为根命名空间前缀(PHP规定根命名空间前缀为反斜杠\),直接触发类解析失败,和Session报错属于同类问题。
解决步骤
按顺序执行以下操作即可修复:
- 进入项目根目录,安装所有依赖:
composer install
- 初始化环境配置文件并生成应用密钥:
cp .env.example .env php artisan key:generate
- 清除所有缓存文件,避免异常缓存干扰类加载:
php artisan config:clear php artisan route:clear php artisan view:clear php artisan cache:clear
- 修正模板中的类调用写法,优先使用全局可用的辅助函数,避免命名空间引入问题:
- 调整
alert.blade.php的Session调用,替换为session辅助函数,无需引入类即可直接使用:
@if ($errors->any()) <div> <ul> @foreach ($errors->all() as $error) <li style="color: red">{{ $error }}</li> @endforeach </ul> </div> @endif @if (session()->has('error')) <div> <ul> <li style="color: red">{{ session('error') }}</li> </ul> </div> @endif @if (session()->has('success')) <div> <ul> <li style="color: green">{{ session('success') }}</li> </ul> </div> @endif- 修正仪表盘模板中Str的错误写法,把所有
/Str::title()替换为str()->title()辅助函数写法,同时删除错误的正斜杠:
@foreach ($products as $product) <a href="/products/view/{{ $product->id }}" class="m-4"> <div class="box-border h-{{ $h_box }} w-{{ $w_box }} p-4 border-4 justify-center rounded-md"> <div class="box-border h-{{ $image_size }} w-{{ $image_size }} border-1 mx-auto"> <img class="object-cover w-full h-full" src="{{ $product->thumb }}"> </div> <div class="box-border h-16 w-{{ $image_size }} border-1 truncate mx-auto"> <h2 class="mt-1 font-medium leading-tight text-base mb-2">{{ str()->title($product->name) }} </h2> @if ($product->price > $product->sale_price) <div class="inline-block"> <h2 class="font-medium leading-tight text-base mt-0 mb-2 text-red-500"> <p class="line-through inline">{{ str()->title($product->price) }}$</p> -> <p class="inline">{{ str()->title($product->sale_price) }}$</p> </h2> </div> @else <h2 class="font-medium leading-tight text-base mt-0 mb-2 text-red-500"> {{ str()->title($product->price) }}$ </h2> @endif </div> <div class="box-border h-16 w-{{ $image_size }} border-1 truncate mx-auto"> <h2 class="mt-1 text-left leading-tight text-base mb-2">0 star | 0 sale </h2> </div> </div> </a> @endforeach - 调整
- 重启开发服务器,刷新页面即可正常访问。
内容的提问来源于stack exchange,提问作者NgDat
相关产品推荐
相关产品推荐

