1

i'm trying to create a ajax controller to switch locate, here are the code:

frontend jquery :

<script language="javascript">
$( "#lang" )
  .change(function () {
        $.ajax({
          method: "POST",
          url: "{{ url('/lang') }}",
          headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
          },
          data: { name: "John", location: "Boston" }
        })
          .done(function( msg ) {
            alert( "Data Saved: " + msg );
        });
  })
</script>

controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class LangController extends Controller
{
    //
    public function Lang () {

        /*$rules = [
            'language' => 'in:en,zh-tw' //list of supported languages of your application.
        ];*/

        App::setLocale('zh-tw');

        return 'success';
    }
}

route:

Route::post('lang', 'LangController@lang');

The result is internal server error 500, the App::setLocale('zh-tw'); run well in other non-ajax controller, everyone know what's wrong with that?

6
  • You can check the logs folder and/or apache2 log to see what actually the error is. Commented Apr 19, 2016 at 5:29
  • i use xampp on windows, but the error log didn't log that. Commented Apr 19, 2016 at 5:45
  • Well generally internal server error is a 5XX level error - generally occurs at server level. So it should have been logged - either in apache or in application. However - you can enable E_ALL in php.ini error setting cause without checking what the error is - it's hard to solve. Commented Apr 19, 2016 at 5:48
  • BTW, stackoverflow.com/a/27474251/926943 seems similar to your problem. You can check that too. Commented Apr 19, 2016 at 5:50
  • 1
    okay in that case you need to use \App:setLocale() instead of App:setLocale(). The difference is the backslash before - which denotes this App is in global namespace outside the namespace of the controller. Commented Apr 19, 2016 at 6:15

1 Answer 1

0

try using \App::setLocale('zh-tw');

because you want to use Illuminate\Support\Facades\App::class but compiler is looking for App class in current namespace. you can also import the class by using use App alias. more on namespace here

Sign up to request clarification or add additional context in comments.

2 Comments

the error is gone, but language switching is not work.
try following the docs exactly, i.e return trans('messages.welcome') instead of 'success' . laravel.com/docs/5.2/localization

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.