第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Laravel 覆蓋渲染器視圖

Laravel 覆蓋渲染器視圖

PHP
交互式愛(ài)情 2022-12-30 17:46:53
我希望只要在有視圖('view')的地方發(fā)送 ajax 請(qǐng)求,而不是返回原始視圖,它會(huì)返回一個(gè) JSON像那樣:/**     * @param \Illuminate\Contracts\View\View $view     * @return \Illuminate\Http\JsonResponse     */    protected function ajaxResponse (View $view)    {        return response()->json([            'id' => $view->getData()['page'],            'title' => $view->getData()['title'],            'content' => $view->renderSections()['content'],            'replacepage' => null,        ]);    }但我絕對(duì)不知道該怎么做我不想在每個(gè)控制器中重復(fù)自己我試圖用我的視圖替換視圖,但它不起作用,因?yàn)槲蚁M磺姓?,但我無(wú)法恢復(fù)我的頁(yè)面部分<?phpnamespace App\Extensions\View;use Illuminate\View\View;class AjaxView extends View{    /**     * Get the contents of the view instance.     *     * @return string     * @throws \Throwable     */    protected function renderContents()    {        if(request()->ajax()) {            echo $this->getResponse($this->getData());            exit;        }        return parent::renderContents();    }    /**     * @param array $data     * @return false|string     */    public function getResponse (array $data = [])    {        return json_encode([            'id' => $data['page'],            'title' => (!empty($data['title']) ? $data['title'] . ' - ' . config('app.name') : null),            'content' => $this->renderSections()['content'],            'replacepage' => null        ]);    }}這將返回:
查看完整描述

2 回答

?
撒科打諢

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊

也許你可以這樣做:在app/Http/Controllers/Controller.php,添加方法response();


protected function view($viewPath, $content, $status = 200, array $headers = [])

{

        if(request()->ajax()) {

            return response()->json($content, $status, $headers);

        }


        return view($viewPath, $content);

}

然后你的控制器方法可以像這樣使用:


public function create()

{

  // logic

  return $this->view('view_path', $data);

}


查看完整回答
反對(duì) 回復(fù) 2022-12-30
?
森林海

TA貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個(gè)贊

我改變了你的功能


protected function ajaxView(string $viewPath, array $content = [], int $status = 200, array $headers = [])

    {

        $view = view($viewPath, $content);


        if(request()->ajax()) {

            dd($view->getData());

            return response()->json([

                'id' => $this->view->getData()['page'],

                'title' => $this->view->getData()['title'],

                'content' => $this->view->renderSections()['content'],

                'replacepage' => null,

            ], $status, $headers);

        }


        return $view;

    }

但是,如果記錄不為空,如果我將 dd() 放在我的外部,則 dd() 記錄數(shù)組為空


查看完整回答
反對(duì) 回復(fù) 2022-12-30
  • 2 回答
  • 0 關(guān)注
  • 146 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)