Kanav
Kanav

Reputation: 2819

Controller method not called in laravel 4

I'm trying to learn laravel 4. I created a form(using view) and returned it via a controller(testController) using index method. I had created this controller using artisan command.

i created another method (dologin) in the controller which would process the form. In the form url parameter i gave the address of dologin method.

This is the route:

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

This is the controller

<?php
class testController extends \BaseController {


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

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

and this is the index view file

 {{ 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() }}

After submitting form, it should echo "working" in the browser. But after submitting the form, page is blank. The url changes though from

/laravel/public/index.php/test/

to

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

Upvotes: 2

Views: 2940

Answers (3)

Gravy
Gravy

Reputation: 12445

umefarooq's answer is correct, however hopefully this answer should give you a bit more insight into getting a head-start in your Laravel development as well as a consistent best-practice programming style.

Firstly, class names should really start with a capital letter. Try to keep methods / function names starting with a lower case letter, and class names starting with a capital.

Secondly, you don't need the \ in front of BaseController. You only need the backslash if you are name-spacing your controller. e.g. if your controller is in the folder Admin\TestController.php, and you put your TestController in the Admin namespace by typing <?php namespace Admin at the beginning of the file. This is when you should use \BaseController because you are telling your TestController to extend BaseController from the Global Namespace. Alternatively, before you declare your class, you can type use BaseController; and you don't need to put a \ in every time.

Specifically related to your question:

When you use resource routes in your routes file, you are telling Laravel that the controller can have any or all of the following methods: index, show, create, store, edit, update and destroy.

As such, Route::resource('test', 'TestController'); will point to TestController.php inside your controllers folder.

Your TestController should be structured as follows, most restful controllers will use the below as some kind of boilerplate:

<?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');
  }
}

Also, the beauty of using Resource Controllers is that you can take advantage of named routes.

in the terminal window, type in php artisan routes.

You should see 7 named routes.

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

So within your form, instead of doing

{{ Form::open(array('url'=>'test/loginform')) }} you can point the url to a named route instead:

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

That way if you ever change the url, or need to move around your site structure, this will be easy, because the forms post url will auto bind to the named route within the routes file. You wont need to update every single one of your views to ensure that the url's are pointing to the correct location.

Finally, as a starting point, I would recommend using JefreyWay/Laravel-4-Generators package. https://github.com/JeffreyWay/Laravel-4-Generators . Use them to create your resources, controllers, views etc. and see how the generators scaffold your models, views and, controllers for you.

Here is another resource to help you get started:

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

Upvotes: 6

Bob Barker
Bob Barker

Reputation: 236

In addition to what umefarooq said, which is 100% accurate. You need to look into flash messages as well.

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');

}

For further research: http://laravel.com/docs/responses

Upvotes: 1

umefarooq
umefarooq

Reputation: 4574

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

will work for RESTful method of controller, like index, edit, destroy, create and now you are using custom method of controller for this you need to create another route

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

hope this will work for you. read route documentation http://laravel.com/docs/routing

Upvotes: 3

Related Questions