Laravel框架中Form表单Get请求搜索(在此感谢[https://simon8.com])

首先看一下HTML部分的Form表单

<form role="search" method="get" id="searchform" action="{{ route('mysearch') }}">
   <input type="search" name="keyword" placeholder="关键字" required>
   <button type="submit"><span class="ion-ios-search-strong"></span></button>
</form>

PHP文件中的路由配置

Route::get('/search','Home\SearchController@index')->name('mysearch');

PHP文件中的控制器部分

复制代码
<?php

namespace App\Http\Controllers\Home;

use App\Http\Model\Home\Article;
use Illuminate\Http\Request;
use App\Http\Requests;


class SearchController extends CommonController
{
    //请求地址
    public function index(Request $request){
        $getKeyWord     = $request->keyword;
        $all_article    = (new Article)->searchArticle($getKeyWord);
//        foreach($all_article as $v){
//                    $v->title       = str_ireplace($getKeyWord,'<font color="#FF0000">'
.$getKeyWord.'</font>',$v->title);
//        }
        return view('home.search.index',compact('all_article','getKeyWord'));
    }
 }
复制代码

要修改的文件路径

J:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Pagination\UrlWindowPresenterTrait.php

首先修改的是第一个方法getLinks

复制代码
protected function getLinks()
{
    $html               = '';
    $url                = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    $pase_url           = parse_url($url);
    $url_query          = '';
    if(strpos($url,'search?keyword') !== false){
        $url_query  = $pase_url['query'];
        $url_query  = strpos($url_query,'&') !== false  ? substr($url_query,0,strpos($url_query,'&'))
         : $url_query;
    }
    if (is_array($this->window['first'])) {   //在测试中,始终走的是当前流程,所以给传了一个字符串类型的参数
        $html .= $this->getUrlLinks($this->window['first'],$url_query);
    }

    if (is_array($this->window['slider'])) {
        $html .= $this->getDots();
        $html .= $this->getUrlLinks($this->window['slider']);
    }

    if (is_array($this->window['last'])) {
        $html .= $this->getDots();
        $html .= $this->getUrlLinks($this->window['last']);
    }
    return $html;
}
复制代码

找到当前类中getUrlLinks方法,然后修改,直接在当前方法内处理返回了

复制代码
protected function getUrlLinks(array $urls,$url_query = '')
{
    $html = '';
    foreach ($urls as $page => $url) {
        $html .= $this->getPageLinkWrapper($url, $page);
    }
    if($url_query != '' && strpos($url_query,'keyword') !== false){

        $html_str       = htmlspecialchars($html);

        if(strpos($html_str,'search?') !== false){
            $html  = htmlspecialchars_decode(str_replace('search?','search?'.$url_query.'&',$html_str));
        }
    }
    return $html;
}
复制代码

经过测试行得通

posted @   温柔的风  阅读(3121)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示