i have read some articles about routing and annotions.
http://mattstauffer.co/blog/laravel-5.0-route-annotations
But since routes.php is removed in Laravel 5 and annotations became the favourite routing part. how can i bind a model to a route using annotation?
In Laravel 5, route model bindings are stored in the before method of the App\Providers\RouteServiceProvider class:
public function before(Router $router, UrlGenerator $url)
{
$router->model('user', 'App\User');
}
This gets called before any of the routes are loaded.
Related
I have an api and some routes are public some need to be protected via auth. I want to have them in one controller class as they are related. I can extend the controller and have beforeRoute function but it runs for any route that is in that controller. is it possible to add a middleware only to specific routes? I'm a js dev and in express I can just pass middleware functions for any route, even multiple middlewares.
class Clanky /*extends \controllers\ProtectedController */{
public function post_novy_clanek(\Base $base) {
//needs to be protected
}
public function get_clanky(\Base $base) {
}
public function get_clanek(\base $base) {
}
public function get_kategorie(\Base $base) {
}
}
PHP is new to me, I just want to know how I can implement the concepts I know from other languages and frameworks in this weird fatfree framework. Thanks.
Use can use f3-access plugin for that purpose https://github.com/xfra35/f3-access
Fatfree is not opinionated about how to do this.. other options to solve this ask might be:
Use php8 attributes on the method and check these in beforeroute.
Consider an own named route naming schema like #admin_routename and apply checking auth in beforeroute
Use f3-middleware plugin and add auth there
Extend an other admin controller that provides auth in beforeroute or use a trait.
I use annotations to define routes in my project. I have a controller method that has such a route:
/**
* #Route("/{slug}", name="showcompany")
*/
public function show(Company $company): Response
{
...
}
This is done to catch requests like website.com/company1, website.com/company2, etc...
I get this error:
"App\Entity\Company object not found by the #ParamConverter
annotation."
Since obviously all other routes such as website.com/signup are considered as slugs of the Company entity.
The question is how to avoid this behavior without moving all routes to routes.yaml and ordering them manually? I want all routes to be prioritized over this /{slug} route.
I have a model called Dbtable which isn't injected when used like this:
public function showEditDbTableForm(Request $request, DbTable $table)
{
}
it only works when I do this:
public function showEditDbTableForm(Request $request, $id)
{
$table = DbTable::find( $id );
}
Same thing happens even when I rename DbTable to DbTble
P.S.: please don't be rude with me as I'm new to Laravel framework
For Implicit Route Model Binding you need to make sure the parameter in the method signature has the same name as the route parameter you want to bind.
Route::get('widgets/{widget}', 'WidgetsController#show');
public function show(Widget $widget)
Laravel automatically resolves Eloquent models defined in routes or controller actions whose type-hinted variable names match a route segment name.
Laravel 5.6 Docs - Routing - Implicit Model Binding
In RouteServiceProvider class add
public function boot()
{
parent::boot();
Route::model('db-table', App\DbTable::class);
// db-table correspond your rout parameter
}
see official documentation https://laravel.com/docs/5.5/routing Explicit Binding section
Using lumen 5.2, I am setting up a GET action that can have resource queries:
GET /expedientes?param1=value1&..paramn=valuen
In my routing I have:
$app->get('expedientes', 'ExpedientesController#index');
In my controller:
class ExpedientesController extends Controller
public function index(Request $request){
if ($request->has('name')) {
return $request->input('name');
}
}
}
The error I obtain:
Declaration of App\Http\Controllers\ExpedientesController::index(Illuminate\Http\Request $request) should be compatible with App\Http\Controllers\Controller::index
The funny thing is, if I rename the controler to a name different from "index", like "indexa" (in both the controller and the routing file) everything works perfectly.
So my question is: Using the standard function name "index" for getting a list of resources, which is the proper way to access the request?
Thanks a lot, regards,
Possible solution #1:
I found out that lumen doesn't use the same controllers as laravel ( lumen: App\Http\Controllers\Controller class not found with fresh install ).
Using
use Laravel\Lumen\Routing\Controller as BaseController;
In the controller so it inherits from lumen's controller
class ExpedientesController extends BaseController
allows me to use a Request parameter in the index method.
But I wonder. Is this the proper way? Even if it is, what would be the way to do this in laravel?
i use laravel 5.2 now.
i have these codes in my routes.php file:
Route::(['dashboard'=>'DashboardArticelController',]);
and laravel generates some router for my app :
GET /dashboard/my-articles App\Http\Controllers\DashboardArticelController#getMyArticles
here is a method in my controller:
public function getMyArticles()
{
//$articels = Auth::user()->articals()->latest('published_at')->get();
//dd(Auth::user()->articals()->latest('published_at')->simplePaginate(3));
$articels = Auth::user()->articals()->latest('published_at')->Paginate(5);
return view('dashboard.view.dashboardArticelEdit',compact('articels'));
}
i wonder how the laravel5 generates this route ,i can not found the method can generates the route with the method name.
By default, Laravel assumes an Eloquent model should map to URL segments using its id column. But what if you expect it to always map to a slug?
Eloquent implements the Illuminate\Contracts\Routing\UrlRoutable contract, which means every Eloquent object has a getRouteKeyName() method on it that defines which column should be used to look it up from a URL. By default this is set to id, but you can override that on any Eloquent model:
class Test extends Model
{
public function getRouteKeyName()
{
return 'slug';
}
}