jasonlewis / enhanced-router

Enhanced Router is an extension to the Laravel 4 router and provides some enhanced functionality.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Form::open does not generate a language prefix using enhanced router.

Xees opened this issue · comments

Goodday, i have the following bit of code for the route:

Route::group(
    array(
        'prefix' => '{locale}'
        ),
    function(){
            Route::post('admin', 'AdminController@postLogin');
    }
)->where('locale', array('en', 'ar'));

Now, this works perfectly fine.

But when i generate a form using Form::open, the arguments 'route' and 'action' of the Form::open function does not do the following:

  1. generate a language prefix and append it to the Form::open. before the route:

    What should happen:

    <form action="http://wscms.dev/en/admin/login">
    

    What is happening:

    <form action="http://wscms.dev/admin/login">
    
  2. Laravel does not detect a route with the Form::open to action, even though the action is correct and exists. this is because no language prefix is specified, and a language prefix is required by the route group using the enhanced router.

When i removed Enhanced router and setted the prefix to null. it worked fine, and it generated a language prefix before every form action.

I'm not sure what the problem is here. I'm using Form::open() with a controllers action and it's working fine.

This is my route group.

Route::group(['prefix' => '{locale}'], function()
{
    Route::get('admin', 'AdminController@getLogin');

    Route::post('login', 'AdminController@postLogin');
})->where('locale', ['en', 'ar']);

Then this is my AdminController.

<?php

class AdminController extends BaseController {

    public function getLogin()
    {
        return View::make('login');
    }

    public function postLogin()
    {
        return 'Logging in!';
    }

}

And here is my simple login form.

{{ Form::open(['action' => ['AdminController@postLogin', 'en']]) }}

    {{ Form::text('username') }}

{{ Form::close() }}

The resulting form open tag looks like this.

<form method="POST" action="http://laravel.dev/en/admin" accept-charset="UTF-8">

I was not using a language prefix in the second param. usually this is not needed for laravel's router. But it seems its needed here.

Yes it's needed because it's a requirement of the route URI. Just like if you had a route like this.

Route::get('user/{id}', 'UserController@getUser');

Using Form::open() would require the ID of the user passed in.

{{ Form::open(['action' => ['UserController@getUser', '2']]) }}

But in normal laravel, You don't need to do so, it automatically applies the language prefix.

Imagine prefixing every single action with the locale you want, and assigning that locale in every controller to an var, and echoing that var on every form open.