【问题标题】:Route Id Not working路由 ID 不起作用
【发布时间】:2016-08-11 19:15:06
【问题描述】:

我正在创建一个名为医生管理系统的项目。我现在陷入困境,当用户登录他的页面时,我的路由功能会获取特定的 ID,然后显示他的信息。但是当我登录时它没有得到 id 这就是为什么它会出现一些错误但是当我手动输入 id 时它工作得很好。我没有找到为什么会发生这种情况。请大家帮帮我。

我的路线文件

Route::group([
    'middleware' => 'auth'
    ], function() {


        Route::get('/home/{profile_id}', [
        'as' => 'admin',
        'uses' => 'AdminController@index'
    ]);

        Route::get('/profile/{profile_id}', array('as' =>'profile' ,'uses' => 'ProfileController@index'));

我的个人资料管理员是

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

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

class ProfileController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function index($profile_id)
    {
        $profile = User::find($profile_id);
        // if(!$profile) {
        //     return redirect()->route('logout')->with(['fail' => 'Profile Not Found']);
        // }
        return view('admin.article.index',['profile' => $profile]);
    }

错误是

【问题讨论】:

    标签: php mysql laravel routes laravel-5.1


    【解决方案1】:

    您没有指定正在访问的 URL,所以我想它是 whatever_your_domain_is/profile/{id}

    出现的错误是告诉您输入的 URL 没有路由。

    我想你想显示登录用户的个人资料,所以实际上你不需要任何 {id} 在路由中,你可以:

    $profile = Auth::user();
    return view('admin.article.index', compact('profile'));
    

    但如果您确实想显示其他用户的个人资料,只需查看您的 URL。

    我注意到的一些事情:

    您的 'profile/{profile_id}' 路线在路线组之外,应该是这样吗?

    选择一种模式来编写您的代码。您使用不同的符号编写数组:array()[]。阅读编码标准(例如https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)。

    【讨论】:

    • 如何使用此表示法更新我的个人资料文件中的任何信息?
    • 你的意思是数组表示法吗? array() 和 [] 的工作方式相同。
    • 我明白了。您可以使用$profile-&gt;name = 'My new name';$profile-&gt;save(); 之类的方式更改任何属性。就是这样。
    • tnx 再次为您提供帮助。
    猜你喜欢
    • 2017-10-31
    • 2019-11-03
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 2018-06-27
    相关资源
    最近更新 更多