简体   繁体   English

laravel无法正确使用网址

[英]laravel cannot use url correctly

I just install laravel environment, I got some problem with url, I do not understand about it. 我只是安装了laravel环境,我在url上遇到了一些问题,对此我不了解。 here it index.php that run on first. 在这里首先运行的index.php。

<a href="pass">pass to page</a>

when I click the pass to page link it maybe go to page that I link to. 当我单击“传递到页面”链接时,它可能会转到我链接到的页面。 but it was 404 not found . 但找不到404。 here is routes.php in app directory 这是应用目录中的routes.php

Route::get('/', 'HomeController@showWelcome');
Route::get('pass', 'PassController@passToPage');

and here is function in PassController 这是PassController中的函数

public function passToPage()
{
    return View::make('pass');
}

I note that I use only '\\'(default directory), I cannot use other directory. 我注意到我只使用'\\'(默认目录),而不能使用其他目录。 thank in advance I am in new laravel. 在此先感谢我在新laravel。

You have tricky part in your code. 您的代码中有棘手的部分。

First you should use slash prefix in your links. 首先,您应该在链接中使用斜杠前缀。 For example: 例如:

<a href="/pass">pass to page</a>

or like this 或像这样

<a href="{{ URL::to('pass', $params) }}">

If you use it without slash the href value could be prepend to current route. 如果使用时不加斜线,则href值可以放在当前路由的前面。 If you use it with slash prefix it will alway points to root endpoint. 如果将其与斜杠前缀一起使用,它将始终指向根端点。

You can consider usig named routes. 您可以考虑使用usig命名路线。 For example 例如

Route::get('pass', [
    'as' => 'pass', 'uses' => 'PassController@passToPage'
]);

and then call this route in your blade view like this: 然后在刀片视图中按以下方式调用此路由:

<a href="{{ URL::route('pass', $params) }}">

$params could be replaced with [] if the is none params 如果没有参数,则可以用[]替换$ params

If the error still ocures the problem is elsewhere. 如果错误仍然存​​在,则问题出在其他地方。

It would be better to used named routes . 最好使用命名路由 This way you can change the URL without modifying the links in your application. 这样,您可以更改URL而无需修改应用程序中的链接。

Route::get('pass', ['uses' => 'PassController@passToPage', 'as' => 'pass']);

<a href="{{ route('pass') }}">pass to page</a>

I solved it with this problem. 我用这个问题解决了。 first time I try direct to this url 第一次,我尝试直接转到该网址

http://localhost/test/ccc/public/pass

it's not work, but when I want to go to page page I need to include index.php as like this 它不起作用,但是当我想转到页面页面时,我需要像这样包含index.php

http://localhost/test/ccc/public/index.php/pass

when I type this, it work but I want to remove index.php from url . 当我键入它时,它可以工作,但是我想从url中删除index.php。 to solve this problem I need to enable mod_rewrite. 为了解决这个问题,我需要启用mod_rewrite。 so I got to httpd.conf, and remove # sign and enable mod_rewrite. 所以我去了httpd.conf,删除#号并启用mod_rewrite。 now it work with this url 现在可以使用该网址

http://localhost/test/ccc/public/pass

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

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