Currenty正在Laravel中开发一个项目,现在想知道当搜索栏出现在每页的导航栏中时,我应该写哪条路线。如果我能在这里得到任何建议,那就太好了。
谢谢!
然而,我将尝试向您介绍Laravel的基本知识:
我假设您在resourcesviewslayouts
中有一个layouts.blade.php
文件,在appViewComponetsAppLayout.php
中有相应的类
或类似的命名
- 您在
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');
}
}
这是您网站上任何页面的基本布局
- 为
routesweb.php
中的第一个页面创建一条路线,以查看新布局
Route::get('/page1',function (){
return view('page1');
});
- 在
resourcesviews
中创建page1.blade.php
page1.blade.php:
<x-index-layout>
I'm Page One
</x-index-layout>
- 为搜索栏创建一个新组件
php artisan make:component SearchBar
用类似的东西编辑resources/views/components/search-bar.blade.php
:
<label for="search" title="Search">
<input type="search" placeholder="Search...">
</label>
- 将组件添加到
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
,搜索栏在顶部
- 在
resourcesviews
中创建具有以下内容的新文件page2.blade.php
<x-app-layout>
I'm Page Two
</x-app-layout>
- 创建其路线:
Route::get('/page2',function (){
return view('page2');
});
- 使用
http://{your-url}/page2
在浏览器中运行它,您会看到顶部带有搜索栏的I'm Page Two
默认的渲染引擎Blade为网站的所有组件使用通用布局,您可以在那里添加搜索栏。
另一个步骤是使搜索栏成为一个组件,并在布局中呈现它