Dodawanie nowych metod do kontrolera zasobów w Laravel
Chcę wiedzieć, czy jest możliwe dodanie nowych metod do kontrolera zasobów w Laravel i jak to zrobić.
Wiem, że te metody są domyślne (index, create, store, edit, update, destroy). Teraz chcę dodać dodatkowe metody i trasy do tego samego kontrolera.
Czy to możliwe?
7 answers
Po prostu dodaj trasę do tej metody oddzielnie, przed zarejestrujesz zasób:
Route::get('foo/bar', 'FooController@bar');
Route::resource('foo', 'FooController');
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-31 00:03:18
Właśnie to zrobiłem, aby dodać metodę GET "delete".
Po utworzeniu plików wystarczy dodać
'AntonioRibeiro\Routing\ExtendedRouterServiceProvider',
Do "dostawcy" w aplikacji / konfiguracji.php
Edytuj Alias trasy w tym samym pliku:
'Route' => 'Illuminate\Support\Facades\Route',
Zmiana na
'Route' => 'AntonioRibeiro\Facades\ExtendedRouteFacade',
I upewnij się, że pliki są automatycznie ładowane, muszą znajdować się w jakimś katalogu, który masz w swoim composerze.json (sekcja" autoload").
Wtedy wystarczy:
Route::resource('users', 'UsersController');
A to (spójrz na ostatnią linijkę) jest wynik po uruchomieniu php artisan routes
:
To są moje pliki źródłowe:
ExtendedRouteFacade.pas
<?php namespace AntonioRibeiro\Facades;
use Illuminate\Support\Facades\Facade as IlluminateFacade;
class ExtendedRouteFacade extends IlluminateFacade {
/**
* Determine if the current route matches a given name.
*
* @param string $name
* @return bool
*/
public static function is($name)
{
return static::$app['router']->currentRouteNamed($name);
}
/**
* Determine if the current route uses a given controller action.
*
* @param string $action
* @return bool
*/
public static function uses($action)
{
return static::$app['router']->currentRouteUses($action);
}
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'router'; }
}
ExtendedRouter.pas
<?php namespace AntonioRibeiro\Routing;
class ExtendedRouter extends \Illuminate\Routing\Router {
protected $resourceDefaults = array('index', 'create', 'store', 'show', 'edit', 'update', 'destroy', 'delete');
/**
* Add the show method for a resourceful route.
*
* @param string $name
* @param string $base
* @param string $controller
* @return void
*/
protected function addResourceDelete($name, $base, $controller)
{
$uri = $this->getResourceUri($name).'/{'.$base.'}/destroy';
return $this->get($uri, $this->getResourceAction($name, $controller, 'delete'));
}
}
ExtendedRouteServiceProvider.pas
<?php namespace AntonioRibeiro\Routing;
use Illuminate\Support\ServiceProvider;
class ExtendedRouterServiceProvider extends ServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app['router'] = $this->app->share(function() { return new ExtendedRouter($this->app); });
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array('router');
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-21 03:26:20
Tak, to możliwe..
W moim przypadku dodaję metodę: data do obsługi request for / data.json w metodzie HTTP POST.
To właśnie zrobiłem.Najpierw rozszerzamy Illuminate\Routing\ResourceRegistrar aby dodać nową metodę data
<?php
namespace App\MyCustom\Routing;
use Illuminate\Routing\ResourceRegistrar as OriginalRegistrar;
class ResourceRegistrar extends OriginalRegistrar
{
// add data to the array
/**
* The default actions for a resourceful controller.
*
* @var array
*/
protected $resourceDefaults = ['index', 'create', 'store', 'show', 'edit', 'update', 'destroy', 'data'];
/**
* Add the data method for a resourceful route.
*
* @param string $name
* @param string $base
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\Route
*/
protected function addResourceData($name, $base, $controller, $options)
{
$uri = $this->getResourceUri($name).'/data.json';
$action = $this->getResourceAction($name, $controller, 'data', $options);
return $this->router->post($uri, $action);
}
}
Następnie utwórz nowy ServiceProvider lub użyj AppServiceProvider .
W metodzie boot dodaj ten kod:
public function boot()
{
$registrar = new \App\MyCustom\Routing\ResourceRegistrar($this->app['router']);
$this->app->bind('Illuminate\Routing\ResourceRegistrar', function () use ($registrar) {
return $registrar;
});
}
Wtedy:
Dodaj do swojej trasy :
Route::resource('test', 'TestController');
Sprawdź przez php artisan route:list
I znajdziesz nową metodę 'data'
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-21 15:01:16
Route::resource('foo', 'FooController');
Route::controller('foo', 'FooController');
Spróbuj .Umieść dodatkowe metody, takie jak getData () itd .itd.. To pomogło mi utrzymać trasę.PHP cleanWarning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-08 13:07:42
To też działa całkiem nieźle. Nie trzeba dodawać więcej tras, wystarczy użyć metody show kontrolera zasobów w ten sposób:
public function show($name){
switch ($name){
case 'foo':
$this -> foo();
break;
case 'bar':
$this ->bar();
break;
defautlt:
abort(404,'bad request');
break;
}
}
public function foo(){}
publcc function bar(){}
Używam domyślnej strony błędu.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-25 06:27:24
Używając Laravel >5 Znajdź sieć.plik php w folderze routes Dodaj swoje metody
Możesz użyć route::resource, aby przekierować wszystkie te metody index, show, store, update, destroy w kontrolerze w jednej linii
Route::get('foo/bar', 'NameController@bar');
Route::resource('foo', 'NameController');
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-23 03:45:06
Wystarczy dodać nową metodę i trasę do tej metody.
W Twoim kontrolerze:
public function foo($bar=“default”)
{
//do stuff
}
I w Twoich trasach internetowych
Route::get(“foo/{$bar}”, “MyController@foo”);
Upewnij się, że metoda w kontrolerze jest publiczna.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-01 06:24:11