刷新内容页面而不刷新 Laravel 中的母版页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33506799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:37:42  来源:igfitidea点击:

Refresh a content page without refreshing master page in laravel

phplaravellaravel-5.1

提问by cyber8200

I have an admin layout

我有一个管理布局

enter image description here

在此处输入图片说明



I have a master layout contain my :

我有一个主布局包含我的:

  • top nav
  • left side nav
  • 顶部导航
  • 左侧导航

Each page is a view, I extend the master layout view, and update only the content section.

每个页面都是一个视图,我扩展了主布局视图,并且只更新了内容部分。

My goal is to refresh onlya content page without refreshing master page.

我的目标是刷新内容页而不刷新母版页。



Is there any laravel packagefor something like this ?

有没有类似这样的Laravel 包

回答by Bogdan

Short answer: use AJAX. There no real need to learn a JavaScript framework if it's that simple (unless you really want to). The below example uses jQuery and assumes you're clicking on the Userslink in the navigation (all links there should have a nav-itemclass so they can be easily identified via that selector) and the link has the attribute href="/admin/users". When clicked it loads the contents in the element with the class content:

简短回答:使用 AJAX。如果 JavaScript 框架那么简单,那么就没有必要去学习它(除非你真的想要)。下面的示例使用 jQuery 并假设您单击导航中的用户链接(那里的所有链接都应该有一个nav-item类,以便可以通过该选择器轻松识别它们)并且该链接具有属性href="/admin/users". 单击它时,它会使用类加载元素中的内容content

$('.item-nav').click(function (event) {
    // Avoid the link click from loading a new page
    event.preventDefault();

    // Load the content from the link's href attribute
    $('.content').load($(this).attr('href'));
});

If the route admin/userswould return the users table with it's contents:

如果路由admin/users将返回带有内容的 users 表:

get('admin/users', function () {
    return view('users')->with('users', Users::all());
});

The above Laravel route example is of course simplified to showcase the idea that each item in your navigation should link to the contents that need to be displayed dynamically.

上面的 Laravel 路由示例当然是简化的,以展示导航中的每个项目都应该链接到需要动态显示的内容的想法。

回答by Leo

You can use renderSections().

您可以使用 renderSections()。

In your main controller action ( say AdminUsersController@getIndex ) you can render a particular section of your view depending on the request. This way if you do an ajax request to the same page you are on you can return only the section you require.

在您的主控制器操作(例如 AdminUsersController@getIndex )中,您可以根据请求呈现视图的特定部分。这样,如果您对所在的同一页面执行 ajax 请求,则只能返回您需要的部分。

 if($request->ajax()) {
         return view('admin.users',$users)->renderSections()['content'];
 }

 return view('admin.users',$users);