我正在使用laravel担任多个角色。我在laravel网站上注册时遵循链接https://dev.to/kaperskyguru/multiple-role-based-authentication-in-laravel-30pc.But。它说:“此页面不起作用。localhost指导了您太多次。 我的路线是:
route::get('/student','studentController@index')->name('student')->middleware('student');
route::get('/teacher','teacherController@index')->name('teacher')->middleware('teacher');
学生中间件文件:
public function handle($request, Closure $next)
{
if (!Auth::check()) {
return redirect()->route('login');
}
if (Auth::user()->role == "STUDENT") {
return redirect()->route('student');
}
if (Auth::user()->role == "TEACHER") {
return redirect()->route('teacher');
}
// return $next($request);
}
LoginController:
protected $redirectTo ;
public function redirectTo()
{
if(Auth::user()->role =="STUDENT")
{
$this->redirectTo = '/student';
return $this->redirectTo;
}
if(Auth::user()->role=="TEACHER")
{
$this->redirectTo = '/teacher';
return $this->redirectTo;
}
}
RegisterController:
protected $redirectTo;
public function redirectTo()
{
if(Auth::user()->role =="STUDENT")
{
$this->redirectTo = '/student';
return $this->redirectTo;
}
// if(Auth::user()->role=="TEACHER")
else
{
$this->redirectTo = '/teacher';
return $this->redirectTo;
}
}
答案 0 :(得分:1)
您的中间件应该具有逻辑路径,该逻辑路径可以使请求继续通过堆栈到达其目的地。您所拥有的只是重定向返回。您还将此中间件分配给了中间件也要重定向到的路由,这将导致无限循环。您可能要进行一些调整:
mkdir -p .obj .obj/examples .obj/tests
i686-w64-mingw32-gcc -g -Wall -MMD -MF .obj/quickjs.check.o.d -Wno-array-bounds -Wno-format-truncation -D_GNU_SOURCE -DCONFIG_VERSION=\"2019-12-21\" -DCONFIG_CHECK_JSVALUE -c -o .obj/quickjs.check.o quickjs.c
i686-w64-mingw32-gcc -g -Wall -MMD -MF .obj/qjs.check.o.d -Wno-array-bounds -Wno-format-truncation -D_GNU_SOURCE -DCONFIG_VERSION=\"2019-12-21\" -DCONFIG_CHECK_JSVALUE -c -o .obj/qjs.check.o qjs.c
qjs.c: In function ‘js_trace_malloc_printf’:
qjs.c:152:35: warning: unknown conversion type character ‘l’ in format [-Wformat=]
printf("H%+06lld.%zd",
^
qjs.c:152:39: warning: unknown conversion type character ‘z’ in format [-Wformat=]
printf("H%+06lld.%zd",
^
qjs.c:152:28: warning: too many arguments for format [-Wformat-extra-args]
printf("H%+06lld.%zd",
^~~~~~~~~~~~~~
qjs.c:152:35: warning: unknown conversion type character ‘l’ in format [-Wformat=]
printf("H%+06lld.%zd",
^
qjs.c:152:39: warning: unknown conversion type character ‘z’ in format [-Wformat=]
printf("H%+06lld.%zd",
^
qjs.c:152:28: warning: too many arguments for format [-Wformat-extra-args]
printf("H%+06lld.%zd",
^~~~~~~~~~~~~~
qjs.c:161:26: warning: unknown conversion type character ‘z’ in format [-Wformat=]
printf("%zd", sz);
^
qjs.c:161:24: warning: too many arguments for format [-Wformat-extra-args]
printf("%zd", sz);
^~~~~
qjs.c:161:26: warning: unknown conversion type character ‘z’ in format [-Wformat=]
printf("%zd", sz);
^
qjs.c:161:24: warning: too many arguments for format [-Wformat-extra-args]
printf("%zd", sz);
^~~~~
qjs.c: In function ‘js_trace_malloc’:
qjs.c:186:35: warning: unknown conversion type character ‘z’ in format [-Wformat=]
js_trace_malloc_printf(s, "A %zd -> %p\n", size, ptr);
^
qjs.c:186:42: warning: format ‘%p’ expects argument of type ‘void *’, but argument 3 has type ‘size_t {aka unsigned int}’ [-Wformat=]
js_trace_malloc_printf(s, "A %zd -> %p\n", size, ptr);
~^
%d
qjs.c:186:31: warning: too many arguments for format [-Wformat-extra-args]
js_trace_malloc_printf(s, "A %zd -> %p\n", size, ptr);
^~~~~~~~~~~~~~~
qjs.c: In function ‘js_trace_realloc’:
qjs.c:216:39: warning: unknown conversion type character ‘z’ in format [-Wformat=]
js_trace_malloc_printf(s, "R %zd %p\n", size, ptr);
^
qjs.c:216:43: warning: format ‘%p’ expects argument of type ‘void *’, but argument 3 has type ‘size_t {aka unsigned int}’ [-Wformat=]
js_trace_malloc_printf(s, "R %zd %p\n", size, ptr);
~^
%d
qjs.c:216:35: warning: too many arguments for format [-Wformat-extra-args]
js_trace_malloc_printf(s, "R %zd %p\n", size, ptr);
^~~~~~~~~~~~
qjs.c:225:35: warning: unknown conversion type character ‘z’ in format [-Wformat=]
js_trace_malloc_printf(s, "R %zd %p", size, ptr);
^
qjs.c:225:39: warning: format ‘%p’ expects argument of type ‘void *’, but argument 3 has type ‘size_t {aka unsigned int}’ [-Wformat=]
js_trace_malloc_printf(s, "R %zd %p", size, ptr);
~^
%d
qjs.c:225:31: warning: too many arguments for format [-Wformat-extra-args]
js_trace_malloc_printf(s, "R %zd %p", size, ptr);
^~~~~~~~~~
i686-w64-mingw32-gcc -g -Wall -MMD -MF .obj/qjs.o.d -Wno-array-bounds -Wno-format-truncation -D_GNU_SOURCE -DCONFIG_VERSION=\"2019-12-21\" -O2 -c -o .obj/qjs.o qjs.c
qjs.c: In function ‘js_trace_malloc_printf’:
qjs.c:152:35: warning: unknown conversion type character ‘l’ in format [-Wformat=]
printf("H%+06lld.%zd",
^
qjs.c:152:39: warning: unknown conversion type character ‘z’ in format [-Wformat=]
printf("H%+06lld.%zd",
^
qjs.c:152:28: warning: too many arguments for format [-Wformat-extra-args]
printf("H%+06lld.%zd",
^~~~~~~~~~~~~~
qjs.c:152:35: warning: unknown conversion type character ‘l’ in format [-Wformat=]
printf("H%+06lld.%zd",
^
qjs.c:152:39: warning: unknown conversion type character ‘z’ in format [-Wformat=]
printf("H%+06lld.%zd",
^
qjs.c:152:28: warning: too many arguments for format [-Wformat-extra-args]
printf("H%+06lld.%zd",
^~~~~~~~~~~~~~
qjs.c:161:26: warning: unknown conversion type character ‘z’ in format [-Wformat=]
printf("%zd", sz);
^
qjs.c:161:24: warning: too many arguments for format [-Wformat-extra-args]
printf("%zd", sz);
^~~~~
qjs.c:161:26: warning: unknown conversion type character ‘z’ in format [-Wformat=]
printf("%zd", sz);
^
qjs.c:161:24: warning: too many arguments for format [-Wformat-extra-args]
printf("%zd", sz);
^~~~~
qjs.c: In function ‘js_trace_malloc’:
qjs.c:186:35: warning: unknown conversion type character ‘z’ in format [-Wformat=]
js_trace_malloc_printf(s, "A %zd -> %p\n", size, ptr);
^
qjs.c:186:42: warning: format ‘%p’ expects argument of type ‘void *’, but argument 3 has type ‘size_t {aka unsigned int}’ [-Wformat=]
js_trace_malloc_printf(s, "A %zd -> %p\n", size, ptr);
~^
%d
qjs.c:186:31: warning: too many arguments for format [-Wformat-extra-args]
js_trace_malloc_printf(s, "A %zd -> %p\n", size, ptr);
^~~~~~~~~~~~~~~
qjs.c: In function ‘js_trace_realloc’:
qjs.c:216:39: warning: unknown conversion type character ‘z’ in format [-Wformat=]
js_trace_malloc_printf(s, "R %zd %p\n", size, ptr);
^
qjs.c:216:43: warning: format ‘%p’ expects argument of type ‘void *’, but argument 3 has type ‘size_t {aka unsigned int}’ [-Wformat=]
js_trace_malloc_printf(s, "R %zd %p\n", size, ptr);
~^
%d
qjs.c:216:35: warning: too many arguments for format [-Wformat-extra-args]
js_trace_malloc_printf(s, "R %zd %p\n", size, ptr);
^~~~~~~~~~~~
qjs.c:225:35: warning: unknown conversion type character ‘z’ in format [-Wformat=]
js_trace_malloc_printf(s, "R %zd %p", size, ptr);
^
qjs.c:225:39: warning: format ‘%p’ expects argument of type ‘void *’, but argument 3 has type ‘size_t {aka unsigned int}’ [-Wformat=]
js_trace_malloc_printf(s, "R %zd %p", size, ptr);
~^
%d
qjs.c:225:31: warning: too many arguments for format [-Wformat-extra-args]
js_trace_malloc_printf(s, "R %zd %p", size, ptr);
^~~~~~~~~~
gcc -g -Wall -MMD -MF .obj/qjsc.host.o.d -Wno-array-bounds -Wno-format-truncation -D_GNU_SOURCE -DCONFIG_VERSION=\"2019-12-21\" -DCONFIG_CC=\"gcc\" -DCONFIG_PREFIX=\"/usr/local\" -O2 -c -o .obj/qjsc.host.o qjsc.c
gcc -g -Wall -MMD -MF .obj/quickjs.host.o.d -Wno-array-bounds -Wno-format-truncation -D_GNU_SOURCE -DCONFIG_VERSION=\"2019-12-21\" -O2 -c -o .obj/quickjs.host.o quickjs.c
gcc -g -Wall -MMD -MF .obj/libregexp.host.o.d -Wno-array-bounds -Wno-format-truncation -D_GNU_SOURCE -DCONFIG_VERSION=\"2019-12-21\" -O2 -c -o .obj/libregexp.host.o libregexp.c
gcc -g -Wall -MMD -MF .obj/libunicode.host.o.d -Wno-array-bounds -Wno-format-truncation -D_GNU_SOURCE -DCONFIG_VERSION=\"2019-12-21\" -O2 -c -o .obj/libunicode.host.o libunicode.c
gcc -g -Wall -MMD -MF .obj/cutils.host.o.d -Wno-array-bounds -Wno-format-truncation -D_GNU_SOURCE -DCONFIG_VERSION=\"2019-12-21\" -O2 -c -o .obj/cutils.host.o cutils.c
gcc -g -Wall -MMD -MF .obj/quickjs-libc.host.o.d -Wno-array-bounds -Wno-format-truncation -D_GNU_SOURCE -DCONFIG_VERSION=\"2019-12-21\" -O2 -c -o .obj/quickjs-libc.host.o quickjs-libc.c
gcc -g -o host-qjsc .obj/qjsc.host.o .obj/quickjs.host.o .obj/libregexp.host.o .obj/libunicode.host.o .obj/cutils.host.o .obj/quickjs-libc.host.o -lm
.obj/quickjs-libc.host.o: In function `js_module_loader_so':
/mnt/c/Users/maple3142/Downloads/quickjs-2019-12-21/quickjs-libc.c:405: undefined reference to `dlopen'
/mnt/c/Users/maple3142/Downloads/quickjs-2019-12-21/quickjs-libc.c:414: undefined reference to `dlsym'
/mnt/c/Users/maple3142/Downloads/quickjs-2019-12-21/quickjs-libc.c:427: undefined reference to `dlclose'
/mnt/c/Users/maple3142/Downloads/quickjs-2019-12-21/quickjs-libc.c:405: undefined reference to `dlopen'
collect2: error: ld returned 1 exit status
Makefile:190: recipe for target 'host-qjsc' failed
make: *** [host-qjsc] Error 1
将此中间件作为“ roleCheck”或任何您喜欢的东西添加到HTTP内核。然后,您可以将此中间件分配为public function handle($request, Closure $next, $role)
{
if (!Auth::check()) {
// no authenticated user
return redirect()->route('login');
}
if (Auth::user()->role != strtoupper($role)) {
// they don't have the role we are checking
// redirect them away to some default location
return redirect()->route('home');
}
// allow the Request to continue to the destination
return $next($request);
}
或roleCheck:student
的路由。
roleCheck:teacher
答案 1 :(得分:0)
此错误是由无限重定向循环引起的。
您应该检查您的路由配置,我认为您(通过中间件)也保护了“登录”路由。您应该为所有类型的用户开放该路由。