【问题标题】:can't able to call controller method based on the given url无法根据给定的 url 调用控制器方法
【发布时间】:2015-03-05 10:30:16
【问题描述】:

我正在使用 Laravel 框架。调用控制器类的方法时遇到问题。

routes.php:

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

它应该调用hello的视图页面。提供网址 http://example.com/laravel 时效果很好。

Route::get('home','HomeController@showWelcome')

当我提供像 http://example.com/laravel/home 这样的 url 时,它不会调用 HomeController 类的方法。

HomeController.php:

 <?php

    class HomeController extends BaseController {

       public function showWelcome()
       {
           return View::make('hello');
       }

    }

 ?>

谁能帮我找出我做错了什么..提前谢谢。

路由代码如下所示:

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

   Route::get('home','HomeController@showWelcome');

?>

【问题讨论】:

  • 这条路线会发生什么:Route::get('/home/','HomeController@showWelcome')?
  • 为什么*/laravel 会通过'/' 路由? (您是否使用带有laravel 作为前缀的路由组?)如果没有,请尝试Route::get('laravel/home', 'HomeController@showWelcome');
  • 你能添加你的路线代码吗?
  • @Set Kyar Wa Lar:我已经在上面发布了路线代码..请查看..
  • 当我给出 home 的 url 名称时,不调用 showWelcome 的控制器方法名称

标签: php laravel


【解决方案1】:

以下文字来自 Laravel 文档:

阿帕奇

框架附带了一个 public/.htaccess 文件,用于允许没有 index.php 的 URL。如果您使用 Apache 来为您的 Laravel 应用程序提供服务,请务必启用 mod_rewrite 模块。

如果 Laravel 附带的 .htaccess 文件不适用于你的 Apache 安装,试试这个:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Nginx

在 Nginx 上,站点配置中的以下指令将允许“漂亮”的 URL:

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

来源:http://laravel.com/docs/4.2/installation#pretty-urls

【讨论】:

  • 当我在网址中使用 laravel/index.php/home 时,得到了答案。但我不想在 url 中使用 index.php .. 为此我能做什么。
  • 我根据 Laravel 安装页面用你需要的信息更新了答案。
猜你喜欢
  • 2021-05-22
  • 1970-01-01
  • 2013-01-20
  • 1970-01-01
  • 1970-01-01
  • 2015-09-12
  • 1970-01-01
  • 2021-03-17
  • 1970-01-01
相关资源
最近更新 更多