哪种途径是在每一页上使用相同的方法进行写入

  • 本文关键字:方法 一页 php laravel
  • 更新时间 :
  • 英文 :


Currenty正在Laravel中开发一个项目,现在想知道当搜索栏出现在每页的导航栏中时,我应该写哪条路线。如果我能在这里得到任何建议,那就太好了。

谢谢!

约翰的回答是绝对正确的。

然而,我将尝试向您介绍Laravel的基本知识:

我假设您在resourcesviewslayouts中有一个layouts.blade.php文件,在appViewComponetsAppLayout.php中有相应的类

或类似的命名

  1. 您在resourcesviewslayouts中的app.blade.php文件,其中包含以下内容:

app.blade.php:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Title of the document</title>
</head>
<body>
<!-- Your Content goes here -->
<div>
{{ $slot }}
</div>
</body>
</html>

AppLayout.php:

<?php
namespace AppViewComponents;
use IlluminateViewComponent;
class AppLayout extends Component
{
/**
* Get the view / contents that represents the component.
*
* @return IlluminateViewView
*/
public function render()
{
return view('layouts.app');
}
}

这是您网站上任何页面的基本布局

  1. routesweb.php中的第一个页面创建一条路线,以查看新布局
Route::get('/page1',function (){
return view('page1');
});
  1. resourcesviews中创建page1.blade.php

page1.blade.php:

<x-index-layout>
I'm Page One
</x-index-layout>
  1. 为搜索栏创建一个新组件
php artisan make:component SearchBar

用类似的东西编辑resources/views/components/search-bar.blade.php

<label for="search" title="Search">
<input type="search" placeholder="Search...">
</label>
  1. 将组件添加到app.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Title of the document</title>
</head>
<body>
<!-- Search Bar -->
<x-search-bar/>
<!-- Your Content goes here -->
<div>
{{ $slot }}
</div>
</body>
</html>

只要你在其他页面上使用<x-app-layout>指令,这将在任何页面上添加你的搜索栏:

http://{your-url}/page1在浏览器中运行它,你会看到I'm Page One,搜索栏在顶部

  1. resourcesviews中创建具有以下内容的新文件page2.blade.php
<x-app-layout>
I'm Page Two
</x-app-layout>
  1. 创建其路线:
Route::get('/page2',function (){
return view('page2');
});
  1. 使用http://{your-url}/page2在浏览器中运行它,您会看到顶部带有搜索栏的I'm Page Two

默认的渲染引擎Blade为网站的所有组件使用通用布局,您可以在那里添加搜索栏。

另一个步骤是使搜索栏成为一个组件,并在布局中呈现它

相关内容

最新更新