Will
Will

Reputation: 681

Basic post route does not work

I am new to laravel 4, and i just create a new project, and do some practice following the official tutorial. For the routing part, there is an example of basic post route

Basic POST Route

Route::post('foo/bar', function()
{
    return 'Hello World';
});

But when i add a post route in my project

Route::post('/'), function()
{
    return 'welcome';
});

It shows "405 - method not allowed" error. why does this happen? post method can not be used to show something?

Upvotes: 0

Views: 79

Answers (1)

Justin Kyle Parton
Justin Kyle Parton

Reputation: 120

To answer your immediate Question:

Post is for sending data to the server, so that the server will perform activity.

You should use this:

Route::get('/', function(){
    return 'welcome';
});

To Provide A Complete Answer

I understand your purpose for doing a return, given that you are new to Laravel. But, let me help you:

Laravel is A MVC framework (Model, View, Controller). A better way to do what your trying to do would be, to add the following:

app/Route.php

Route::controller tell laravel that any request that is directed at that specific URI, the controller will handle it.

Route::controller('SOME_URI', 'HomeController');

app/controllers/HomeController.php

Look here under the heading "Actions Handled By Resource Controller". the table column "action" shows the "default function names" that laravel recognizes. A Get request results in laravel using the index function, as seen below:

class HomeController extends BaseController {

    public function index(){

        return View::make('home.index');

    }
}

app/views/index.php

Put whatever Html you want.

if you type in the full url. you will get the content thats in the view/index.php. I may not be politically correct.. But Views handle Output, Controllers handle Logic, Models handle manipulation (DB,etc).

Upvotes: 1

Related Questions