控制器在Laravel的route.php中不起作用


Controller doesn't work in route.php in Laravel

我正在研究一个工作正常的项目,我现在已经恢复了项目的备份,项目和备份是相同的,我还将备份名称更改为原始名称,现在route.php中的控制器不起作用。

For do the backup and restore it I did

cp -r project/ project_backup
rm -r project
cp -r project_backup/ project

在我的routes.php中我有:

<?php
/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/
Route::get("/password", function(){
        $password = Hash::make('12345678');
        echo $password;
    });
Route::get('/login', 'UserController@getLogin');

当我在web浏览器中输入/密码时,它的工作很好。输出为:

$2y$10$.grNtTpANndXN0V3/rn9buSO470jPEeVWVyc00rupU6iNt9G.DMZC

但是当我输入/login时,我得到

得到http://project/public/index.php/login [HTTP/1.0 500内部服务器错误247ms]

在我的UserController中我有:

public function getLogin(){
        Log::info("getLogin");
        $password = Hash::make('12345678');
        echo $password;
    }

实际上,laravel.log不记录任何内容。这个函数似乎没有执行。

为什么控制器不工作?

我的laravel版本是:

Laravel Framework version 5.2.30

感谢编辑:UserController.php代码
<?php
namespace Ordered'Http'Controllers;
use Ordered'User;
use Illuminate'Http'Request;
use Ordered'Http'Requests;
use Auth;
use Log;
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return 'Illuminate'Http'Response
     */
    public function index()
    {
        //
        $user = User::all();
        return response()->json(["error"=>false, "data"=>$user->load("bars")],200);
    }
    public function getLogin(){
        Log::info("getLogin");
        $password = Hash::make('12345678');
        echo $password;
    }
    public function postLogin(Request $request){
        $input = $request->all();
        Log::info($input["email"]);
        Log::info($input["password"]);
        if (Auth::attempt(['email' => $input["email"], 'password' => $input["password"]], true))
        {
            Log::info("postInfo");
            return redirect()->intended('/tables');
        }
    }
    public function apiLogin(Request $request){
        $input = $request->all();
        if (Auth::attempt(['username' => $request->header("u"), 'password' => $request->header("p")]))
        {
            return response()->json(["error"=>false, "data"=>Auth::user()->load("bars")],200);
        }
        return response()->json(["error"=>true, "data"=>""],401);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return 'Illuminate'Http'Response
     */
    public function create()
    {
        //
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  'Illuminate'Http'Request  $request
     * @return 'Illuminate'Http'Response
     */
    public function store(Request $request)
    {
        //
    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return 'Illuminate'Http'Response
     */
    public function show($id)
    {
        //
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return 'Illuminate'Http'Response
     */
    public function edit($id)
    {
        //
    }
    /**
     * Update the specified resource in storage.
     *
     * @param  'Illuminate'Http'Request  $request
     * @param  int  $id
     * @return 'Illuminate'Http'Response
     */
    public function update(Request $request, $id)
    {
        //
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return 'Illuminate'Http'Response
     */
    public function destroy($id)
    {
        //
    }
}

您使用错误的地址访问/login路由,它应该是:

 http://project/login

如果它不工作,你还需要设置web服务器通过设置文档根到laravel_project/public目录。

:

  • 检查/storage/logs/laravel.log是否有错误
  • 设置storage文件夹及其中所有文件和文件夹的正确权限
  • 清除应用和路由缓存:

php artisan cache:clear

php artisan route:clear

您正在使用Hash facade,尽管您没有在use语句中声明它的使用。

PHP试图解析"App'Http'Controllers"命名空间中的类,因此试图找到不存在的App'Http'Controllers'Hash类。

在文件中添加use Hash;应该可以解决您的问题。

use Auth;
use Log;
use Hash;