How to Call Subfolder Controller method in Routes in CodeIgniter 4 - codeigniter

I have controllers in subfolders
--Controllers
----Admin
--------UserController
--------AccountController
----User
--------UserController
--------AccountController
I write routes for it
$routes->group('user', function ($routes){
$routes->get('dashboard', 'UserDashboard::index');
$routes->get('changePassword', 'User\AccountController::changePassword');
});
It give me 404 even though I have method and have something output as well
Controller code
<?php namespace App\Controllers;
use App\Controllers\BaseController;
class AccountController extends BaseController
{
public function index()
{
echo "Hello";
exit();
return view('user/account/changePassword');
}
public function changePassword()
{
echo "Change Password View";
exit();
return view('user/account/changePassword');
}
}

You need to change the namespace of your controllers.
User controllers
<?php namespace App\Controllers\User;
use App\Controllers\BaseController;
class AccountController extends BaseController
{
}
Admin controllers
<?php namespace App\Controllers\Admin;
use App\Controllers\BaseController;
class AccountController extends BaseController
{
}

Related

Controller not found media model in laravel 8

I tested to using laravel but i get some error unexpected, i already follows tutorial and some video. i think, i do it right. but i get this error. The problem controller not found media model.
Class "App\Media" not found
Route
Route::get('/', [MediaController::class, 'index']);
MediaController
namespace App\Http\Controllers;
use App\Media;
class MediaController extends Controller
{
public function index(){
return Media::all;
}
}
Media model
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Media extends Model
{
protected $table = 'media';
public $timestamps = false;
protected $primaryKey = 'Media_ID';
}
Your use statement for your Media model is incorrect. As of Laravel 8 the default namespace for models is App\Models where previously they where in the App namespace.
namespace App\Http\Controllers;
use App\Models\Media;
class MediaController extends Controller
{
public function index(){
return Media::all;
}
}
Ensure that the Media.php file exists in your app/Models directory and is capitalised correctly.
Then in your web.php file, don't forget to include the use statement for your MediaController at the top.
use App\Http\Controllers\MediaController;
Use this code:
Your Controller:
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class MediaController extends Controller {
public function index() {
return Media::all;
}
}
Your Route:
Route::get('/', 'MediaController#index');

How to test cookie which created by service in Laravel

route
Route::get('/token', [\App\Http\Controllers\TokenController::class, 'index']);
TokenController.php
namespace App\Http\Controllers;
use App\Services\Token;
class TokenController extends Controller
{
public function index()
{
$tokenService = app(Token::class);
return $tokenService->handle('https://www.nba.com');
}
}
Token Service
namespace App\Services;
class Token
{
public function handle(string $redirect)
{
return redirect($redirect)->withCookie(cookie('token', 'abcd'));
}
}
I want to leave a token in cookies when the user enters the page and redirect to another URL, I am sure the cookie was saved when I check on the browser, but I don't know to do the test that token service did save the cookie in the unit test.
first try
namespace Tests\Unit;
use App\Services\Token;
use Illuminate\Support\Facades\Cookie;
use Tests\TestCase;
class TokenTest extends TestCase
{
public function testToken()
{
app(Token::class)->handle('https://www.nba.com');
dd(Cookie::get('token')); // null
}
}
second try
namespace Tests\Unit;
use App\Services\Token;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Tests\TestCase;
class TokenTest extends TestCase
{
public function testToken()
{
app(Token::class)->handle('https://www.nba.com');
Route::get('view-cookie', function (Request $request) {
return $request->cookie('token');
});
$response = $this->get('/view-cookie');
dd($response->getContent()); // ""
}
}

Fresh laravel lumen cannot find controller reside under subfolder of controllers

Fresh laravel lumen 5.8 installed but controller name cannot be found in route
I created UserController in App\Http\Controllers\User
User Controller Content:
namespace App\Http\Controllers;
class UserController extends Controller {
public function index() {
return 'User list';
}
}
Loaded in route:
$router->get('/user', 'User\UserController#index')
Error:
Class App\Http\Controllers\User\UserController does not exist
I also tried with absolute namespace App\Http\Controllers\User\UserController#index but it still doesn't work.
It works if I don't put UserController in User folder
Change your UserController's namespace to:
namespace App\Http\Controllers\User;
class UserController extends Controller
{
// ...
}
Try changing namespace of the controller to
namespace App\Http\Controllers\User;
class UserController extends Controller
{
// ...
}

Laravel 5.6 error in One to Many relationship

My One to Many relationship is like One Sura has many ayah.
Sura Model is like below
Sura.php
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class Sura extends Model
{
public function ayahs()
{
return $this->hasMany('App\Model\Quran', 'surah_id', '_id');
}
}
Quran Model is like below
Quran.php
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class Quran extends Model
{
}
Sura Controller is like below
SuraController.php
<?php
namespace App\Http\Controllers;
use App\Model\Sura;
use Illuminate\Http\Request;
class SuraController extends Controller
{
public function show(Sura $sura)
{
return Sura::find($sura)->ayahs();
}
}
My route is like below
api.php
Route::apiResource('/suras', 'SuraController');
I am getting error like below
I am trying to browse using below URL.
http://127.0.0.1:8000/api/suras/2
SuraController.php
<?php
namespace App\Http\Controllers;
use App\Model\Sura;
use Illuminate\Http\Request;
class SuraController extends Controller
{
public function show($id)
{
return Sura::find($id)->ayahs();
}
}

How to use namespaces in case of inheritance?

I am new to OOPS thus want to clarify things. I have this code below which works perfectly fine.
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function index()
{
echo "admin controller";
}
}
Now I don't intend to use use keyword as it is going to be used once also wanted to experiment and thus, I used the code below.
namespace App\Http\Controllers\Admin;
class AdminController extends App\Http\Controllers\Controller
{
public function index()
{
echo "admin controller";
}
}
Now the above mentioned code without use keyword throws a fatal error exception. Why does that happen? In theory,I think I am doing exactly what is supposed to be done then why the exception?
You will have to import it from global namespace , below code will work fine
namespace App\Http\Controllers\Admin;
class AdminController extends \App\Http\Controllers\Controller
{
public function index()
{
echo "admin controller";
}
}
Use keyword import class from global namespace, but
class AdminController extends App\Http\Controllers\Controller
it will import parent class relative you current namespace (namespace App\Http\Controllers\Admin) , so translated path will be: App\Http\Controllers\Admin\App\Http\Controllers\Controller which is invalid.

Resources