简体   繁体   English

Laravel 5主页控制器

[英]Laravel 5 home page controller

After login it will redirect to http://localhost/laravel/public/home 登录后,它将重定向到http:// localhost / laravel / public / home

So view file location will be \\resources\\views\\home.blade.php. 因此,查看文件的位置将是\\ resources \\ views \\ home.blade.php。

Now on this home page i'm able to get login user id 现在,在此主页上,我可以获得登录用户ID

<?php
echo $id = Auth::id();
?>

now i have created controller using 现在我已经使用创建了控制器

D:\wamp\www\laravel>php artisan make:controller HomeController

Controller created successfully. 控制器创建成功。

Now on home page i have to show some data on basis of logged in user. 现在在主页上,我必须根据登录用户显示一些数据。

In home controller if i do echo exit, but it's not working. 在家庭控制器中,如果我做回声出口,但它不起作用。 So in which controller file i have to write code? 那么我必须在哪个控制器文件中编写代码?

HomeController.php HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class HomeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }
}

You can access to the user data this way 您可以通过这种方式访问​​用户数据

 {{ Auth::user()->field_name }}

For example: 例如:

{{ Auth::user()->id }}
{{ Auth::user()->name }}
{{ Auth::user()->email }}

first you may need to create a route in routes.php 首先,您可能需要在routes.php中创建一条路线

Route::get('laravel/public/home', [
'as' => 'home', 'uses' => 'HomeController@index'
]);

and then add code in your HomeController's index() function. 然后在您的HomeController的index()函数中添加代码。

In your HomeController.php add the below lines to make use of it. 在您的HomeController.php添加以下HomeController.php行以使用它。

use Auth;

So, Your HomeController.php will look like this 因此,您的HomeController.php将如下所示

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;

And Now, you can do return Auth::user()->id from your controller to return the Loggedin user's Id. 现在,您可以从控制器return Auth::user()->id来返回Loggedin用户的ID。

It will look like this 看起来像这样

public function index()
{
return Auth::user()->id;
}

Note : 注意 :

You can see the Auth::user()->id only if the user is get authenticated. 只有在验证用户身份后,您才能看到Auth::user()->id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM