×ðÁú¿­Ê±

ÔõÑùʹÓÃLaravelÀ´ÑéÖ¤Óû§ÊÇ·ñµÇ¼

laravel ÊÇÏÖÔÚ×îÊ¢ÐÐµÄ php ¿ò¼ÜÖ®Ò»¡£ËüÌṩÁËÐí¶àÊÊÓù¤¾ßºÍº¯Êý£¬Ê¹¿ª·¢Ö°Ô±Äܹ»¿ìËÙ¹¹Á¢¹¦Ð§Ç¿Ê¢µÄ web Ó¦ÓóÌÐò¡£ÔÚÐí¶à web Ó¦ÓóÌÐòÖУ¬Óû§ÐèÒªµÇ¼£¬ÒÔ±ã»á¼ûÊܱ£»¤µÄÒ³Ã棬ÎÒÃÇÐèҪѧϰÔõÑùʹÓà laravel À´ÑéÖ¤Óû§ÊÇ·ñµÇ¼¡£

ÔÚ Laravel ÖУ¬Óû§ÈÏ֤ϵͳÊÇÓɹٷ½µÄ Laravel Auth ¾ÙÐÐÖÎÀíµÄ£¬Õâ¸öϵͳÌṩÁËÐí¶à³£¼ûµÄÈÏÖ¤¹¦Ð§£¬ÀýÈçÓû§×¢²á¡¢µÇ¼ºÍÃÜÂëÖØÖõÈ¡£ÔÚÕâ¸öϵͳÖУ¬ÎÒÃÇʹÓà Middleware À´ÑéÖ¤Óû§ÊÇ·ñÒѾ­µÇ¼¡£Middleware ÊÇÒ»¸öÇëÇó¹ýÂËÆ÷£¬ÓÃÓÚÔÚÇëÇóµÖ´ïÖ¸¶¨Â·ÓÉ֮ǰִÐÐһЩ²Ù×÷¡£ÎÒÃÇ¿ÉÒÔʹÓà Middleware À´ÑéÖ¤Óû§ÊÇ·ñÒѾ­µÇ¼£¬ÒÔ±ãÎÒÃÇÄܹ»ÏÞÖÆÓû§»á¼ûÊܱ£»¤µÄÒ³Ãæ¡£

ÏÂÃ棬ÎÒ½«ÏòÄãչʾÔõÑùʹÓà Middleware À´ÑéÖ¤Óû§ÊÇ·ñÒѾ­µÇ¼¡£ÎÒÃÇ¿ÉÒÔʹÓà Laravel ×Ô´øµÄ make:middleware ÏÂÁîÀ´½¨ÉèÒ»¸öÐ嵀 Middleware£¬ºÃ±ÈÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸öÃûΪ “AuthMiddleware” µÄÖÐÐļþ¡£

ÎÒÃÇ¿ÉÒÔÔËÐÐÒÔÏÂÏÂÁîÀ´½¨Éè AuthMiddleware£º

php artisan make:middleware AuthMiddleware

µÇ¼ºó¸´ÖÆ

ÔËÐÐÉÏÊöÏÂÁîºó£¬Laravel »áÔÚ app/Http/Middleware Ŀ¼Öн¨ÉèÒ»¸ö AuthMiddleware.php Îļþ¡£´Ëʱ£¬ÎÒÃÇÐèÒªÔÚÕâ¸öÎļþµÄ handle ÒªÁìÖбàд×ðÁú¿­Ê±ÑéÖ¤Âß¼­¡£ÏÂÃæÊÇÒ»¸öʾÀý£º

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class AuthMiddleware
{
    public function handle($request, Closure $next)
    {
        if (!Auth::check()) {
            return redirect('/login');
        }

        return $next($request);
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄÀý×ÓÖУ¬ÎÒÃÇÊ×ÏÈʹÓà Auth::check() ÒªÁìÀ´¼ì²éÓû§ÊÇ·ñÒѾ­µÇ¼¡£ÈôÊÇÓû§Ã»ÓеǼ£¬ÎÒÃǽ«Öض¨Ïòµ½ /login Ò³Ãæ¡£ÈôÊÇÓû§ÒѾ­µÇ¼£¬ÎÒÃǽ«ÔÊÐíÇëÇó¼ÌÐøÖ´ÐУ¬Õâ¾ÍÊÇÎÒÃÇËùνµÄ¡¸Í¨¹ý Middleware¡¹¡£

ÏÂÒ»²½£¬ÎÒÃÇÐèÒªÔÚ×ðÁú¿­Ê±Â·ÓÉÖÐʹÓà AuthMiddleware¡£ÎÒÃÇ¿ÉÒÔÔÚ Laravel µÄ·Óɽç˵ÖÐʹÓà Middleware µÄ·½·¨ÓÐÁ½ÖÖ£º

ÒªÁìÒ»£ºÔÚ·Óɽç˵ÖÐʹÓà Middleware

Route::get('/dashboard', function () {
    return view('dashboard');
})-&gt;middleware('auth');

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓà auth ÖÐÐļþÀ´ÑéÖ¤Óû§ÊÇ·ñÒѾ­µÇ¼¡£µ±Óû§»á¼û /dashboard Ò³Ãæʱ£¬ÈôÊÇÓû§Ã»ÓеǼ£¬Ëü½«±»Öض¨Ïòµ½ /login Ò³Ãæ¡£

ÒªÁì¶þ£ºÔÚ¿ØÖÆÆ÷ÀàÖÐʹÓà Middleware

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class DashboardController extends Controller
{
    public function __construct()
    {
        $this-&gt;middleware('auth');
    }

    public function index()
    {
        return view('dashboard');
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇÔÚ¿ØÖÆÆ÷ÖÐʹÓà AuthMiddleware£¬µ±Óû§»á¼û /dashboard ʱ£¬ÈôÊÇÓû§Ã»ÓеǼ£¬Ëü½«±»Öض¨Ïòµ½ /login Ò³Ãæ¡£

ÏÖÔÚ£¬ÎÒÃÇÒѾ­Ñ§»áÁËÔõÑùʹÓà Laravel À´ÑéÖ¤Óû§ÊÇ·ñÒѾ­µÇ¼£¬ÎÒÃÇ¿ÉÒÔÔÚ¿ØÖÆÆ÷ºÍ·ÓÉÖÐʹÓà Middleware£¬ÒÔ±ãÏÞÖÆÓû§»á¼ûÊܱ£»¤µÄÒ³Ã档ͨ¹ý Middleware£¬ÎÒÃÇ¿ÉÒÔÈÃ×ðÁú¿­Ê±Ó¦ÓóÌÐòÔ½·¢Çå¾²ºÍ¿É¿¿¡£

ÒÔÉϾÍÊÇÔõÑùʹÓÃLaravelÀ´ÑéÖ¤Óû§ÊÇ·ñµÇ¼µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±ÊµÊ±ÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿