Symfony Routing: Can call route but not forward to - internationalization

I have a route for just the locale without any other information (the startpage that is):
homepage:
pattern: /{_locale}/
defaults: { _controller: OurStartBundle:Default:index }
If I call the route directly it works (i.e.: localhost/de_DE/) but if I forward it throws the Error:
Unable to parse the controller name "/app_dev.php/de_DE/".
I forward using the Controller Method like this:
$locale = \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
return $this->forward($this->generateUrl('homepage', array('_locale' => $locale)));
Anyone jave any idea why this doesn't work?

You are trying to forward the request to a URI, rather than a bundle string.
For example, your forward call should be...
$response = $this->forward('OurStartBundle:Default:index', array(
'_locale' => 'de_DE'
));

The Problem was that i used $this->forward() where i wanted a redirect and should have used $this->redirect().
Solution is:
$locale = \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
return $this->redirect($this->generateUrl('OurStartBundle_homepage', array('_locale' => $locale)));

Related

Laravel 5.3 dynamic routing to multiple controllers

I'm using Laravel 5.3. I have a bunch of urls that I'd like to handle with a single route, to multiple controllers.
e.g.
GET /admin/foo => FooController#index
GET /admin/foo/edit/1 => FooController#edit($id)
GET /admin/bar => BarController#index
GET /admin/bar/edit/1 => BarController#item($id)
GET /admin/baz => BazController#index
GET /admin/baz/edit/1 => BazController#item($id)
etc.
I want to be able to detect if the controller exists, and if not throw a 404 or route to a default controller (which may throw a 404).
Below is what I've got so far, but I'm not sure what I'm doing. Shouldn't I be instantiating the controller using the service container? I don't think I should be hardcoding namespaces like this. And my handling of the id parameter is sketchy. Perhaps I should have two routes for these two patterns or something?
Route::get('/admin/{entityType}/{action?}/{id?}', function ($entityType, $action = 'index', $id = null) {
$controllerClass = 'App\Http\Controllers\\' . ucfirst($entityType) . 'Controller';
$controller = new $controllerClass;
$route = app(\Illuminate\Routing\Route::class);
$container = app(\Illuminate\Container\Container::class);
return (new Illuminate\Routing\ControllerDispatcher($container))->dispatch($route, $controller, $action);
abort(404);
});
I'd recommend you to define a route for every controller explicitly. This is the best way to build a maintainable app.
Also, if using one route and one method is an option (with right architecure it is) use one route:
Route::get('/admin/{entityType}/{action?}/{id?}', 'Controller#method');
And one entry point:
public function method($entity, $action = null, $id = null)
{
// Handle request here.
https://laravel.com/docs/5.3/routing#parameters-optional-parameters

Why aren't my controllers actions accessible with unit tests in Laravel

I have set up my route as such:
Route::controller('clients', 'Controllers\ClientsController');
Through this method I can easily access all the controller functions via post and get. However I cannot test them as easily.
public function testCantDeleteOtherAccountsClient()
{
Route::enableFilters();
$user = Models\User::find(1);
$this->be($user);
$response = $this->action('GET', 'ClientsController#getDelete');
$this->assertRedirectedToAction('ClientsController#getIndex');
}
This test results in the message
InvalidArgumentException: Route [ClientsController#getDelete] not defined.
The method accessible via url though. What am I missing?
Just tried this out myself (a route specified via the controller) your issue is that using action requires a named route. Controller routes do not currently support this as far as I'm aware of.
If you create a test route:
Route::get('test', array(
'as' => 'testName',
'uses' => 'ClientsController#getDelete'
));
And try
$this->action('GET', 'testName');
The test should pass, you can view all the routes with names via php artisan routes.
You may want to use $this->client->request() instead. You can check if a redirect occurred with:
$this->assertRedirectedTo("some\url");
Note that $this->call() is just an alias to $this->client->request().
I found changing it to use call instead of action worked for me:
$response = $this->call('GET', 'clients/delete/1');

Is there a way to set a default url parameter for the Route class in Laravel 4?

I'm trying to set up sub-domain based routing in Laravel 4 and I've hit a bit of an annoyance...
My route group looks like this:
Route::group(array('domain' => '{company}.domain.com'), function() {
// ...
});
Which seems to work fine, however, I need to specify the company parameter for every route/url I generate. I.e:
{{ HTML::linkRoute('logout', 'Logout', ['company' => Input::get('company')]) }}
Is there any way to specify the company parameter as static/global, so it is automatically added to any links I specify, unless otherwise overwritten/removed?
Unfortunately, no (I haven't seen any evidence in the router or HTMLBuilder that you can). You could, however, make an HTML macro... Example:
HTML::macro('lr', function($link, $title) {
$company = !empty(Input::get('company')) ? Input::get('company') : "";
return HTML::linkRoute($link, $title, ['company' => $company]);
});
Then call it - instead of HTML::linkRoute, use HTML::lr('logout', 'Logout')
Just an idea.

Laravel 4 parameter as controller action

I would like to know if there is a possibility in Laravel 4 to actually call function of controller based on parameter given. For example if I have route like :
'auth/{action}
then is there a way to call controller action based on 'action' param ? In Kohana I could write something like:
'auth/<action>' -> defaults (controller=>'UserController',action=>'<action>'
Well not exacly like that but you know what I mean :) Anyway if there is no chance to do that then do I have to split my route to single routes ?
Sounds like you just need to route to the controller with auth being the base URI.
Route::controller('auth', 'AuthController');
This controller (AuthController) now expects your methods to be prefixed with the HTTP verb they should respond to. You can also use the getIndex method to respond to the base URI, which in this case is auth.
An example controller might look something like this:
class AuthController extends Controller {
public function getIndex()
{
return 'Index page'; // Responds to localhost/auth
}
public function getLogin()
{
return 'Login page'; // Responds to localhost/auth/login
}
}
There is one thing you should be aware of. If you do Route::controller('/', 'HomeController'); then it should be LAST. Any routes after it will not get called because of Laravel automatically adding a "missing method" route that will catch anything that isn't matched by a routable method on the controller.
More on RESTful controllers at the official documentation.
I haven't tested this exact code, but I used something similar and it worked:
Route::any('auth/{action}', function($action){
$controller = new UserController();
$controller->$action();
});
You might find you also need to handle parameters, like this:
Route::any('auth/{action}/{param}', function($action, $param){
$controller = new UserController();
$controller->$action($param);
});
You could even tweak it to cover all your controllers:
Route::any('{controller}/{action?}/{param?}', function($controller,$action='index',$param=null)
{
$controller = str_replace(' ', '', ucwords(str_replace('-', ' ', $controller))).'Controller';
$controller = new $controller;
$action = lcfirst(str_replace(' ', '', ucwords(str_replace('-', ' ', $action))));
return $controller->$action($param);
});
If you wanted to have an index action with a parameter, that wouldn't work, but otherwise, it seems to work well enough. It's also not going to handle a second parameter, if you were wanting to do that.
Lots of ways you could extend this idea.
Jason's answer is more correct, (matches the docs, cleaner code, etc.) but if you didn't want to think about HTTP methods, or you wanted a sort of master route to handle nearly every request, this is an option.

Call a controller in Laravel 4

In Laravel 3, you could call a controller using the Controller::call method, like so:
Controller::call('api.items#index', $params);
I looked through the Controller class in L4 and found this method which seems to replace the older method: callAction(). Though it isn't a static method and I couldn't get it to work. Probably not the right way to do it?
How can I do this in Laravel 4?
You may use IoC.
Try this:
App::make($controller)->{$action}();
Eg:
App::make('HomeController')->getIndex();
and you may also give params:
App::make('HomeController')->getIndex($params);
If I understand right, you are trying to build an API-centric application and want to access the API internally in your web application to avoid making an additional HTTP request (e.g. with cURL). Is that correct?
You could do the following:
$request = Request::create('api/items', 'GET', $params);
return Route::dispatch($request)->getContent();
Notice that, instead of specifying the controller#method destination, you'll need to use the uri route that you'd normally use to access the API externally.
Even better, you can now specify the HTTP verb the request should respond to.
Like Neto said you can user:
App::make('HomeController')->getIndex($params);
But to send for instance a POST with extra data you could use "merge" method before:
$input = array('extra_field1' => 'value1', 'extra_field2' => 'value2');
Input::merge($input);
return App:make('HomeController')->someMethodInController();
It works for me!
bye
This is not the best way, but you can create a function to do that:
function call($controller, $action, $parameters = array())
{
$app = app();
$controller = $app->make($controller);
return $controller->callAction($app, $app['router'], $action, $parameters);
}
Route::get('/test', function($var = null) use ($params)
{
return call('TestController', 'index', array($params));
});
Laurent's solution works (though you need a leading / and the $params you pass to Request::create are GET params, and not those handled by Laravel (gotta put them after api/items/ in the example).
I can't believe there isn't an easier way to do this though (not that it's hard, but it looks kinda hackish to me). Basically, Laravel 4 doesn't provide an easy way to map a route to a controller using a callback function? Seriously? This is the most common thing in the world...
I had to do this on one of my projects:
Route::controller('players', 'PlayerController');
Route::get('player/{id}{rest?}', function($id)
{
$request = Request::create('/players/view/' . $id, 'GET');
return Route::dispatch($request)->getContent();
})
->where('id', '\d+');
Hope I'm missing something obvious.
$request = Request::create('common_slider', 'GET', $parameters);
return Controller::getRouter()->dispatch($request)->getContent();
For laravel 5.1
It's an Old question. But maybe is usefull. Is there another way.
In your controller: You can declare the function as public static
public static function functioNAME(params)
{
....
}
And then in the Routes file or in the View:
ControllerClassName::functionNAME(params);

Resources