当前位置: 首页 > 知识库问答 >
问题:

Laravel 5.2-中间件验证

唐法
2023-03-14

我刚刚安装了laravel 5.2,创建了auth register、login和reset password,但现在我想为我的项目创建一个索引,所有用户(也没有登录)都可以访问。我试图创造

路由::get(“/”,HomeController@home');

但此视图仅对登录的用户启用。

我的路线

Route::auth();
Route::get('/home', 'HomeController@index');
// POST - FORM CREA 
Route::get('/crea-regalo', 'PostController@form');
Route::post('/crea-regalo', 'PostController@creaPost');
// LISTA ANNUNCI PRINCIPALE
Route::get('/', 'HomeController@home');

我的家庭控制器

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $posts = Post::orderBy('id','DESC');
        return view('home', compact('posts'));
    }

    public function home()
    {
        $posts = Post::all();
        return view('index', compact('posts'));
    }
}

如何创建所有用户都可以访问的视图路径?

谢谢你的帮助!

共有2个答案

姚臻
2023-03-14

将您希望只允许经过身份验证的用户的路由放在中间件auth中,如下所示:

Route::group(['middleware' => ['auth']], function () {
  //your routes    
})

对于所有用户都可以访问的路由,将其放在上述组之外。

佟和平
2023-03-14

您好,请编写单独的控制器来访问所有页面,因为您已经在Constructor中编写了auth中间件

public function __construct()
{
   $this->middleware('auth');
}

类似的像

class GuestController extends Controller
{

    public function __construct()
    {

    }


    public function home()
    {
        $posts = Post::all();
        return view('index', compact('posts'));
    }
}
Route::get('/home', 'GuestController@home');

或者你可以这样做

$this->middleware('auth', ['except' => ['home']]);

这将能够访问所有人的主页。在构造函数中添加

public function __construct()
{
   $this->middleware('auth', ['except' => ['home']]);
}
 类似资料:
  • Key Auth 中间件 Key Auth 中间件提供了一个基于 key 的验证方式。 对于有效的 key,它将调用下一个处理程序。 对于无效的 key,它发送”401 - Unauthorized”的响应。 对于空的 key,它发送”400 - Bad Request”。 使用 e.Use(middleware.KeyAuth(func(key string) bool { return k

  • 我的路线是这样的: 这个路由检查,如果用户是登录之前调用免费下载方法.如果不是,登录表单出现。 然后用户需要登录并且登录控制器返回家并且用户需要再次点击路线按钮('product.free')以便访问路线名称product.free。 有一种方法调用ProductController@freeDownload方法只是在登录后,如果用户点击按钮之前? 希望我或多或少是清楚的。 这里我的登录控制器:

  • null 这在2.0中可以使用,但如果令牌无效(上面的步骤2)并且声明从未添加,我会得到“no authenticationScheme was specified,and there was no DefaultChallengeScheme found”。 所以现在我读到了2.0版本中身份验证的改变: https://docs.microsoft.com/en-us/aspnet/core/m

  • 中间件 captcha 用于为 Macaron 实例 提供验证码服务。 GitHub API 文档 下载安装 go get github.com/go-macaron/captcha 使用示例 想要使用该中间件,您必须同时使用 cache 中间件。 // main.go import ( "github.com/go-macaron/cache" "github.com/go-ma

  • 概览 首先同步下项目概况: 上篇文章分享了,路由中间件 - Jaeger 链路追踪(实战篇),文章反响真是出乎意料, 「Go中国」 公众号也转发了,有很多朋友加我好友交流,直呼我大神,其实我哪是什么大神,只不过在本地实践了而已,对于 Go 语言的使用,我还是个新人,在这里感谢大家的厚爱! 这篇文章咱们分享:路由中间件 - 签名验证。 为什么使用签名验证? 这个就不用多说了吧,主要是为了保证接口安全

  • 中间件 binding 为 Macaron 实例 提供了请求数据绑定与验证的功能。 GitHub API 文档 下载安装 go get github.com/go-macaron/binding 使用示例 获取表单数据 假设您有一个联系人信息的表单,其中姓名和信息为必填字段,则我们可以使用如下结构来进行表示: type ContactForm struct { Name