Laravel 路由不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24378833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:41:34  来源:igfitidea点击:

Laravel Routing Does not Work

laravellaravel-routing

提问by Jishad

I am new in LARAVEL framework and I want to run a controller with just function of view a page

我是 LARAVEL 框架的新手,我想运行一个仅具有查看页面功能的控制器

class TestController extends BaseController {

    public function index()
    {
        return View::make('hai');
    }
}

I set the routing in routes.php file as given below

我在 routes.php 文件中设置了路由,如下所示

Route::get('test','TestController@index');

I tried to run in mozilla with

我试图在 mozilla 中运行

localhost/laravel/public/test

but it shows

但它显示

Not Found
The requested URL /laravel/public/test was not found on this server.

is there any problem in my .htaccess page ?

我的 .htaccess 页面有问题吗?

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ / [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

Any body please help me.

任何机构请帮助我。

回答by IamGhale

I'm assuming that you are on Ubuntu machine. To make it work, at first, enable the rewrite module by executing following command in the terminal

我假设你在 Ubuntu 机器上。为了让它工作,首先,通过在终端中执行以下命令来启用重写模块

 sudo a2enmod rewrite

Second, find "apache2.conf" file in your system, mine is located in

其次,在你的系统中找到“apache2.conf”文件,我的位于

/etc/apache2/apache2.conf

In this file, find the following snippets of code:

在此文件中,找到以下代码片段:

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>

Change "AllowOverride None" to "AllowOverride All". Save the file and restart the apache server by executing following command

将“AllowOverride None”更改为“AllowOverride All”。保存文件并通过执行以下命令重新启动apache服务器

sudo service apache2 restart

This should solve the issue. Cheers

这应该可以解决问题。干杯

回答by Justin Kyle Parton

Precondition: you will know that you have htaccess working, if when you first installed laravel and went to localhost/laravel/public/, you'd see the laravel logo.

先决条件:你会知道你有 htaccess 工作,如果你第一次安装 laravel 并转到localhost/laravel/public/,你会看到 laravel 标志。

The route is:

路线是:

Route::get('/', function()
{
    return View::make('hello');
});

Regarding Your route: you don't need to explicitly state the function (@index) in the route. Laravel has a default path it follows based on the HTTP request type. Check here(under the heading Actions Handled By Resource Controller) for more details on that.

关于您的路线:您不需要在路线中明确说明功能(@index)。Laravel 有一个基于 HTTP 请求类型的默认路径。查看此处(在标题为资源控制器处理操作下)了解更多详细信息。

回答by tisuchi

Simply I refer you not modifying .htaccess file. From http://tisuchi.com/easy-way-handle-routing-laravel/:

我只是指您不要修改 .htaccess 文件。从http://tisuchi.com/easy-way-handle-routing-laravel/

Static Page Handling

Handling static page in laravel is one of the easiest way to handle route. Open app/routes.php file and deleted everything from that. Assume that, we are try to route into our following pages- home (example.com), about (example.com/about), contact (example.com/contact). To do so, just write following code in your routes.php file-

/**
 * Static page code
 */

Route::get('/', function(){
    return View::make('home');
});

# For example.com/about
Route::get('about', function(){
    return View::make('about');
});


/**
 * Naming Route for static Page
 */
# For example.com/contact
Route::get('contatct', array('as' => 'contact', function(){
 return View::make('contact');
}));

静态页面处理

在 laravel 中处理静态页面是处理路由的最简单方法之一。打开 app/routes.php 文件并从中删除所有内容。假设,我们尝试路由到我们的以下页面 - 主页(example.com),关于(example.com/about),联系人(example.com/contact)。为此,只需在您的 routes.php 文件中编写以下代码-

/**
 * Static page code
 */

Route::get('/', function(){
    return View::make('home');
});

# For example.com/about
Route::get('about', function(){
    return View::make('about');
});


/**
 * Naming Route for static Page
 */
# For example.com/contact
Route::get('contatct', array('as' => 'contact', function(){
 return View::make('contact');
}));

If you are really new in Laravel, highly recommend to look into following 2 tutorials-

如果您真的是 Laravel 的新手,强烈建议您查看以下 2 个教程-

Ofcourse, don't forget to take a look into official page of laravel.com. Hope, it will work for you.

当然,别忘了看看 laravel.com 的官方页面。希望,它会为你工作。

Have fun...

玩得开心...