使用Laravel Framework 5.2.31版和Apache / 2.4.12(Ubuntu)服务器。得到以下错误消息:
404 Not Found在此服务器上找不到请求的URL / laravel / public / signup。
感谢您的帮助。
下面是routes.php(该文件不再准确。请参阅编辑部分下面的新文件)
<?php
Route::group(['middleware' => ['web']], function()
{
Route::get('/', function ()
{
return view('welcome');
});
Route::post('/signup', [
'uses' => 'UserController@postSignUp',
'as' => 'signup'
]);
});
以下是UserController.php文件
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function postSignUp(Request $Request)
{
$email = $request['email'];
$prenom = $request['prenom'];
$motDePasse = bcrypt($request['motDePasse']);
$user = new User();
$user->email = $email;
$user->prenom = $prenom;
$user->motDePasse = $motDePasse;
$user->save();
return redirect()->back();
}
public function postSignIn(Request $Request)
{
}
}
修改
以下是routes.php
<?php
Route::get('/', function ()
{
return view('welcome');
});
Route::post('/signup', [
'uses' => 'UserController@postSignUp',
'as' => 'signup'
]);
以下是RouteServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function boot(Router $router)
{
//
parent::boot($router);
}
/**
* Define the routes for the application.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function map(Router $router)
{
$this->mapWebRoutes($router);
//
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
protected function mapWebRoutes(Router $router)
{
$router->group([
'namespace' => $this->namespace, 'middleware' => 'web',
], function ($router) {
require app_path('Http/routes.php');
});
}
}
答案 0 :(得分:1)
首先,您似乎在post
中使用routes.php
方法,但是您尝试使用get
方法加载路线。
其次,您的Web服务器配置错误。 Laravel项目的Web服务器should be pointed to a public
directory(您应该为它创建VirtualHost并重新启动Web服务器)。
此外,从web
移除routes.php
中间件,因为it will cause errors。
答案 1 :(得分:0)
将此添加到.htaccess
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
更改路径:
打开local / bootstrap / paths.php
'public' => __DIR__.'/../public', to 'public' => __DIR__.'/../../',
打开local / bootstrap / paths.php并找到下面的代码
&#39;公共&#39; =&GT;的 DIR 强>&#39; /../公共&#39 ;, 用以下代码更改
&#39;公共&#39; =&GT;的 DIR 强>&#39; /../../ 39;,
Open index.php (on root) and find below code
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/start.php';
使用以下代码进行更改
require __DIR__.'/local/bootstrap/autoload.php';
$app = require_once __DIR__.'/local/bootstrap/start.php';
现在你已经设置了所有设置,你可以访问网址http://example.com/如果仍然有问题尝试清除浏览器缓存。