【问题标题】:Controller method not called in laravel 4laravel 4中未调用控制器方法
【发布时间】:2014-05-02 20:07:36
【问题描述】:

我正在尝试学习 laravel 4。我创建了一个表单(使用视图)并使用 index 方法通过控制器(testController)返回它。我使用 artisan 命令创建了这个控制器。

我在控制器中创建了另一个方法(dologin)来处理表单。在表单 url 参数中我给出了 dologin 方法的地址。

这是路线:

Route::resource('test', 'testController');

这是控制器

<?php
class testController extends \BaseController {


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

    public function dologin(){
        echo "working";
    }

这是索引视图文件

 {{ Form::open(array('url'=>'test/loginform')) }}
        {{ Form::text('username', null, array('placeholder'=>'Username')) }}<br/>
        {{ Form::password('password', array('placeholder'=>'Password')) }}<br/>

        {{ Form::submit('Login') }}
    {{ Form::close() }}

提交表单后,它应该在浏览器中回显“工作”。但是提交表单后,页面是空白的。网址更改为

/laravel/public/index.php/test/

/laravel/public/index.php/test/loginform

【问题讨论】:

    标签: laravel


    【解决方案1】:

    umefarooq 的回答是正确的,但希望这个回答能让您更深入地了解如何在 Laravel 开发中抢占先机,并获得一致的最佳实践编程风格。

    首先,类名应该以大写字母开头。尽量保持方法/函数名以小写字母开头,类名以大写字母开头。

    其次,您不需要 BaseController 前面的\。如果您对控制器进行名称间距,则只需要反斜杠。例如如果您的控制器位于文件夹 Admin\TestController.php 中,并且您通过在文件开头键入 &lt;?php namespace Admin 将您的 TestController 放在 Admin 命名空间中。这是您应该使用\BaseController 的时候,因为您告诉您的TestController 从全局命名空间扩展BaseController。或者,在你声明你的类之前,你可以输入use BaseController;,你不需要每次都输入\

    与您的问题特别相关:

    当你在你的路由文件中使用资源路由时,你是在告诉 Laravel 控制器可以有以下任何或所有方法:indexshowcreatestoreeditupdatedestroy

    因此,Route::resource('test', 'TestController'); 将指向控制器文件夹中的 TestController.php。

    您的 TestController 的结构应如下所示,大多数 restful 控制器将使用以下作为某种样板:

    <?php
    
    class TestController extends BaseController
    {
      public function __construct()
      {
      }
    
      // Typically used for listing all or filtered subset of items
      public function index()
      {
        $tests = Test::all();
        return View::make('test.index', compact('tests'));
      }
    
      // Typically shows a specific item detail
      public function show($id)
      {
        $test = Test::find($id);
        return View::make('test.show', compact('test'));
      }
    
      // Typically used to show the form which creates a new resource.
      public function create()
      {
        return View::make('test.create');
      }
    
      // Handles the post request from the create form
      public function store()
      {
        $test = new Test;
    
        $test->attribute1 = Input::get('attribute1');
        $test->attribute2 = Input::get('attribute2');
        $test->attribute3 = Input::get('attribute3');
        $test->attribute4 = Input::get('attribute4');
    
        if ($test->save())
        {
          return Redirect::route('test.show', $test->id);
        }
      }
    
      // Shows the edit form
      public function edit($id)
      {
        $test = Test::find($id);
        return View::make('test.edit', compact('test'));
      }
    
      // Handles storing the submitted PUT request from the edit form.
      public function update($id)
      {
        $test = Test::find($id);
        $test->attribute1 = Input::get('attribute1');
        $test->attribute2 = Input::get('attribute2');
        $test->attribute3 = Input::get('attribute3');
        $test->attribute4 = Input::get('attribute4');
    
        if ($test->save())
        {
          return Redirect::route('test.show', [$id]);
        }
      }
    
      // Used to delete a resource.
      public function destroy($id)
      {
        $test = Test::find($id);
        $test->delete();
        return Redirect::route('test.index');
      }
    }
    

    此外,使用资源控制器的好处在于您可以利用命名路由。

    在终端窗口中,输入php artisan routes

    您应该会看到 7 条命名路线。

    test.index
    test.destroy
    test.show
    test.edit
    test.destroy
    test.create
    test.update
    

    所以在你的表单中,而不是做

    {{ Form::open(array('url'=&gt;'test/loginform')) }} 你可以将 url 指向一个命名路由:

    {{ Form::open(array('route' => array('test.store')) }}
    

    这样,如果您更改了 url,或者需要在站点结构中移动,这将很容易,因为表单发布 url 将自动绑定到路由文件中的命名路由。您无需更新每一个视图以确保 url 指向正确的位置。

    最后,作为起点,我建议使用 JefreyWay/Laravel-4-Generators 包。 https://github.com/JeffreyWay/Laravel-4-Generators 。使用它们来创建您的资源、控制器、视图等,并查看生成器如何为您构建模型、视图和控制器。

    这里是另一个帮助您入门的资源:

    https://laracasts.com/lessons/understanding-rest

    【讨论】:

    • 太好了,谢谢。并且不要忘记选择您的正确答案。 :)
    【解决方案2】:
    Route::resource('test', 'testController'); 
    

    适用于控制器的 RESTful 方法,例如索引、编辑、销毁、创建,现在您正在使用控制器的自定义方法,为此您需要创建另一个路由

     Route::post("test/loginform",'testController@dologin');
    

    希望这对你有用。阅读路线文档http://laravel.com/docs/routing

    【讨论】:

      【解决方案3】:

      除了 umefarooq 所说的,这是 100% 准确的。您还需要查看 Flash 消息。

      public function dologin(){
          //do login verification stuff
          If login validated
          Return redirect::to(logged/page)->with('message', 'You're logged in');
      
          If login failed
          Return redirect::to('test')->with('message', 'You login credentials fail');
      
      }
      

      进一步研究: http://laravel.com/docs/responses

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-05
        • 2016-04-04
        • 2014-03-09
        • 2014-05-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多