Laravel Controller Subfolder - laravel

I tried to get the following curl statement work.
curl -X GET http://localhost:8080/api/v1/admin/user
In routes.php
Route::resource('admin.user', 'admin/UserController');
Route::get('/admin/user/{userid}', 'UserController#getAdminUser');
Route::get('/admin/user', 'UserController#index');
controllers/admin/UserControler.php
<?php namespace Controllers\Admin;
class UserController extends \BaseController {
public function index()
{
return DB::table('users')->get();
}
public function getAdminUser($userid)
{
return DB::table('users')->where('userid', $userid)->get();
}
}
After I run the curl command, all list of user do not show up. Please give me advise.

Related

How import data using controller to view in Laravel 8.*

I would like to go to specific object view from main page(index). Here is how I communicate with page which is about to be showed
{{ route('object',['id'=>$object->id]) }}
Error message:
Error message says that it should be something wrong with controller, but in previous case this code worked.
web.php(routes)
Route::get('/object/{id}','FrontendController#object')->name('object');
My FrontendController
use App\Interfaces\FrontendRepositoryInterface;
class FrontendController extends Controller
{
public function __construct(FrontendRepositoryInterface $frontendRepository)
{
$this->fR = $frontendRepository;
}
public function index()
{
$objects = $this->fR->getObjectsForMainPage();
//dd($objects);
return view('frontend.index',['objects'=>$objects]);
}
public function object($id)
{
$object = $this->fR->getObject($id);
//dd($objects);
return view('frontend.object',['object'=>$object]);
}
FrontendRepository
use App\Models\EventObject;
use App\Models\Photo;
use App\Interfaces\FrontendRepositoryInterface;
class FrontendRepository implements FrontendRepositoryInterface {
public function getObjectsForMainPage()
{
return EventObject::with(['photos','clubs'])->get();
}
public function getObject($id)
{
return EventObject::with(['photos','clubs'])->get();
}
}
Solution was to clear routes cache using php artisan route:cache and to add missing / in route
Route::get('/object/'.'{id}','FrontendController#object')->name('object');

CodeIgniter controller doesn't recognize function

This is something strange.
The controller function called product just doesn't work, but any other name works. See the following code:
namespace App\Controllers;
class Shop extends BaseController {
public function index() {
return view('shop');
}
public function asdf(){
return view('product');
}
public function product(){
return view('product');
}
}
Calling the function asdf (or whatever name it is), works. But I get a 404 if I call the /shop/product.
How to explain (and hopefully solve) it?

Laravel Resource Routing not showing anything when directly call method

I am stuck in resource routing
when I enter url netbilling.test/customer it goes to customer index file but when I enter url netbilling.test/customer/index nothing is returned. Also guide me if I have to route different method than in resource what is the method for that.
here is my web.php,
Route::get('/dashboard', function () {
return view('dashboard/index');
});
Route::resource('/customer','CustomerController');
here is my customer controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Customer;
use App\Package;
use Redirect,Response;
class CustomerController extends Controller
{
public function index()
{
$packages = Package::get();
$customers = Customer::orderBy('id', 'DESC')->get();
return view('customer/index', compact('customers','packages'));
}
public function create()
{
//
}
public function store(Request $request)
{
//
}
public function show($id)
{
//
}
public function edit($id)
{
//
}
public function update(Request $request, $id)
{
//
}
public function destroy($id)
{
}
}
Without custom route specification, this is how the index route maps to a Resource Controller, taken from Actions Handled By Resource Controller:
Verb
URI
Action
Route Name
GET
/photos
index
photos.index
So if you want URI /customer/index to work, then you need to specify this explicitly in your Controller:
use App\Http\Controllers\CustomerController;
Route::resource('customer', CustomerController::class);
Route::get('customer/index', [CustomerController::class, 'index'])->name(customer.index);

getJson in test in laravel does not get passed to controller

I have a test in a Laravel project where I do a getJson request and some answer should be returned. But the method in the controller doesn't get hit.
The test
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class NotificationsTest extends TestCase
{
use DatabaseMigrations;
public function setUp(): void
{
parent::setUp();
$this->signIn();
}
public function test_a_user_can_fetch_their_unread_notifications()
{
create(DatabaseNotification::class);
$response = $this->getJson(url('/profiles') . '/' . auth()->user()->name . '/notifications')->json();
$this->assertCount(1, $response);
}
The line in webp.php that should process this request:
Route::get('/profiles/{user}/notifications', 'UserNotificationsController#index');
The UserNotificationsController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\User;
class UserNotificationsController extends Controller
{
public function __construct()
{
$this->middelware('auth');
}
public function index() {
dd(" UsderNotificationsController-Index method hit");
return auth()->user()->unreadNotifications;
}
public function destroy(User $user, $notificationId)
{
dd(' Destroy method hit');
auth()->user()->notifications()->findOrFail($notificationId)->markAsRead();
}
}
If I run the test with phpunit, I would expect that the DD() in the index method should be executed. But it doesn't.
I tried all kinds of variations to generate the URI, but always get the same result. Can anyone tell me why I do not generate the correct URI?
Kind regards,
HUbert
//start by doing that : in your controller
Route::get('/profiles/notifications', 'UserNotificationsController#index');
public function test_a_user_can_fetch_their_unread_notifications()
{
$this->withoutHandlingException();
create(DatabaseNotification::class,['user_id'=>$this->signIn()->id]);
$this->signIn()//i think it should return authenticated user
$response = $this->get('/profiles/notifications')
->assertStatus(200);
// $this->assertCount(1, $response);
}
-//in your index function
public function index() {
dd(" UsderNotificationsController-Index method hit");
return response()->json(auth()->user()->unreadNotifications,200);
}

NotFoundHttpException for Controller route in Laravel 4

I am trying to get started using controllers in Laravel 4 and I am running into some trouble. Here is the basic run down:
I have a controller in the controllers folder called FansController in the file FansController.php:
<php
class FansController extends BaseController {
public $restful = true
public function getindex() {
return View::make('fans.landing');
}
}
Within my views folder, I have a folder called "fans" that controls a view file "landing.blade.php". It contains simple html: <h1>hello</h1>
In my routes.php file, I have a route calling the controller. Here is that code:
Route::get('landing', array('uses' => 'FansController#index'));
When I visit the url: public/fans/landing
I receive a "NotFoundHttpException";
Do you have any ideas what may be going wrong? Thank you for your help.
First off you need to specify what version of Laravel you are running.
If you are running Laravel 4 then:
// routes.php
Route::get('/', array('uses' => 'GuestController#getIndex'));
// GuestController.php
class GuestController extends BaseController {
public function getIndex() {
return 'Hello world.';
}
}
Then run $ composer dump-autoload -o or php composer.phar dump-autoload -o (if your composer is installed locally) on your CLI
In laravel 3, however
// routes.php
Route::get('/', array('uses' => 'GuestController#index'));
// GuestController.php
class GuestController extends BaseController {
public $restful = true;
public function get_index() {
return 'Hello world.';
}
}
Probably you need to change your method name to index:
class FansController extends BaseController {
public $restful = true
public function index() {
return View::make('fans.landing');
}
}

Resources