How to access to laravel routing resource methods? - php

I am using laravel to have access to my database. I have used the command php artisan make:controller CategoriesController --resource in the terminal to create a class where I can access different methods in one route.
The routing code for the class is: Route::apiResource("categories", CategoriesController::class);. With /categories I can get to the index() method (via get), where I can show my table values. But I do not know how I can use other methods. For example I have created test() with a simple return ["Result"=>"Working"].
I have tried /categories/test /categoriestest /categories%test but I can not show the result from test().
Simple routing works fine when I use specific routes for every method, but I want to make a more clear code so I would like to use the --resource to only have one route.

Related

Class 'App\Http\Controllers\rimdetails' not found?

I have created a laravel project with Rims and tyres. when clicking a Rim from the index page, i would like to link directly to a rim detail page, like a productdetails page based on the clicked product.
So far, it seems that my route and blade files are fine! but i have no idea how to make the controller function. i have tried with:
public function show($id)
{
$rimdetails = rimdetails::with('rimdetails')->findOrFail($id);
return View::make('rimdetails', compact($rimdetails));
}
and getting the error :
Class 'App\Http\Controllers\rimdetails' not found
try this one
for laravel 6
Route::get('rims','App\Http\Controllers\RimDetails#show');
for laravel 8
use App\Http\Controllers\RimDetails';
Route::get('rims',[RimDetails#show,'show']);
and also make sure you have exact name of file as you have in controller
You need to alias this rimdetails class if you want to reference it correctly. Currently you are in a declared namespace and referencing a class, rimdetails. PHP thinks you are referring to a class named rimdetails in the current namespace App\Http\Controllers so its looking for App\Http\Controllers\rimdetails as per the error.
Add this below the namespace declaration and before the class definition:
use App\rimdetails;
Assuming your model is in the app folder.

hidden magic in Laravel blade components

I had anonymous component resources\views\components\homepage\feedback.blade.php to render feedback on homepage. From the beginning it was just html. Then I decided to connect Class file. I already had another View Class component and I just copied it manually instead of using artisan command.
App\View\Components\Feedback.php
namespace App\View\Components;
use Illuminate\View\Component;
use App\Models\Feedback;
class Feedback extends Component
{
public $feedbacks;
public function __construct()
{
$this->feedbacks = Feedback::wherePublished(true)->take(5);
}
public function render()
{
return view('components.homepage.feedback');
}
}
And then {{ dd($feedbacks) }} in view file gives me error that this variable is not defined.
Undefined variable: feedbacks (View: C:\laragon\www\lara7\resources\views\components\homepage\feedback.blade.php)
If I try to create Test component with artisan command and put this code inside it works, but then I cannot rename it back to Feedback class. It gives me error
Symfony\Component\ErrorHandler\Error\FatalError
Cannot declare class App\View\Components\Feedback because the name is already in use
But old class already deleted, so I cannot understand what is wrong.
It seems like there is some hidden link between View Class and Blade components, which needs to be erased. But where is this link located?
When switching component type from anonymous to class and back, you have to clear compiled view files:
php artisan view:clear
That's because Laravel incorporate specific component type invocation into the compiled view code.
I found problem.
I got $feedbacks is undefined, because my anonymous component without variables initially was located in resources\views\components\homepage\feedback.blade.php and when I decide to create View Class for this component there was no link established.
Laravel creates automatic link between feedback.blade.php and app\View\FeedbackComponent.php only when blade file located directly in resources\views\components folder. And my component was in subfolder.
So laravel tried to render resources\views\components\homepage\feedback.blade.php with $feedback variable inside and it cannot find where $feedback is defined.
So I just manually register FeedbacksComponent class like that in appservice provider boot method
Blade::component('homepage-feedbacks', FeedbacksComponent::class);
and then use <x-homepage-feedbacks/> to render it
I would say documentation is not very clear. It says that outside of components folder automatic discovery is not working.
But it doesn't say that inside components subfolder automatic discovery is not working.
In Laravel 8 you can use and no need to declare the component
<x-homepage.feedback />
I think you're right I have been having the same issue and and I've been really struggling with it. Finally I found a workaround which is if you change the file name it works so I think it's a problem with the laravel framework and I think they need to address this issue

How to call createDeleteForm in CRUD controller for another entity

I have a basic CRUD controller using the "php bin/console generate:doctrine:crud AppBundle:Product" command. So I have the basic generated productController.php along with a Product.php entity that I made before, the generated CRUD product views, etc.
In ProductController.php there is a showAction method that has this line:
$deleteForm = $this->createDeleteForm($product);
I am trying to create a settings controller that embeds the product's showAction method but, as you can guess, trying to use
$this->createDeleteForm($product) yields
"Attempted to call an undefined method named "createDeleteForm" of class "AppBundle\Controller\SettingsController""
What's the best way to incorporate a settings controller and, if I'm on the right track, how can I generate the delete form for the product within the settings controller?
Thanks so much for any help you can offer!

Laravel controller route

In Laravel 5.1 I was able to create following route:
Route::controller('posts', 'PostsController');
It was very handy, since I could use methods depending on request type:
public function getCreate()
{
// method for getting
}
public function postCreate()
{
// method for creating
}
In Laravel 5.5 it appears that this functionality (HTTP Controllers) has been removed(?) and replaced by HTTP Requests.
Requests are nice, but not that handy.. and it offers way more methods than I need.
Is there a possibility to keep using request-related method names for controllers in Laravel 5.5?
I think you can use Resource Route
Resource Controllers
Laravel resource routing assigns the typical "CRUD" routes to a controller with a single line of code. For example, you may wish to create a controller that handles all HTTP requests for "photos" stored by your application. Using the make:controller Artisan command, we can quickly create such a controller:
php artisan make:controller PhotoController --resource
This command will generate a controller at app/Http/Controllers/PhotoController.php. The controller will contain a method for each of the available resource operations.
Next, you may register a resourceful route to the controller:
Route::resource('photos', 'PhotoController');
This single route declaration creates multiple routes to handle a variety of actions on the resource. The generated controller will already have methods stubbed for each of these actions, including notes informing you of the HTTP verbs and URIs they handle.
Actions Handled By Resource Controller
Ref:
https://laravel.com/docs/5.5/controllers#resource-controllers
Route::controller() was eliminated after Laravel 5.3
Route::resource() is very specific to exactly, only create for you and let you access the seven methods to CRUD an object
If you want to create your own views, I believe you have to define all them with Route::get(), Route::post(), etc. in the routes/web file

Laravel routes with multiple optional parameters

I am building a RESTful api using Laravel. I am confused on how to do the routing.
I have the following api controller
class APIController extends BaseController{
public function sendMsg($authid, $roomid, $msg){
}
public function getMsg($roomid, $timestamp){
}
}
The URL format I want this to be accessible looks like this:
http://example.com/api/{functionName}/{parameter1}/{parameter2}/.../
Here, in the first parameter, I will have the function name which should map to the function in the controller class and following that the parameters the controller needs.
For example To access the sendMsg() function, the url should look like this:
http://example.com/api/sendMsg/sdf879s8/2/hi+there+whats+up
To access the getMsg() function, the url should look like
http://example.com/api/getMsg/2/1395796678
So... how can I write my routes so that it can handle the dynamic number and different parameters need?
I can write one route for each function name like so:
Route::get('/api/sendmsg/{authid}/{msg}', function($authid, $msg){
//call function...
});
and same for the other function. This if fine but is there a way to combine all function to the APIController in one route?
Yes, you can combine all the function to your APIController in one route by using a resourceful controller which is best suited for building an API:
Route::resource('api' ,'APIController');
But, technically, it's not one route at all, instead Laravel generates multiple routes for each function, to check routes, you may run php artisan routes command from your command prompt/terminal.
To, create a resourceful controller you may run the following command from your command line:
php artisan controller:make APIController
This will create a controller with 6 functions (skeleton/structure only) and each function would be mapped to a HTTP verb. It means, depending on the request type (GET/POST etc) the function will be invoked. For example, if a request is made using http://domain.com/api using GET request then the getIndex method will be invoked.
public function getIndex()
{
// ...
}
You should check the documentation for proper understanding in depth. This is known as RESTful api.

Categories