So in my project I have one class that is placed in app folder (not inside app/http/controllers) and has middleware just App.
When I'm trying to get its method from routes.php it's not found because it's not in controllers folder and middleware.
What should I write instead of Route::get('/get', 'MyApi#get'); to make it all work?
First of all, for your API you should use something like Lumen, or, if you want it inside your main project, make nested folder inside your Controllers folder, and access it from routes like Api\MyApi#get.
If you really want it outside Controllers folder (absolutely no reason for it, IMHO), you need to change RouteServiceProvider namespace setting, to be empty string:
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* #var string
*/
protected $namespace = '';
// ...
}
Then specify full namespace for your controller and access it from wherever you want.
You can organize the controllers in subfolders if you namespace them correctly. I can't think of a valid reason to not put application controllers in the application its controllers directory.
See:
Route to controller in subfolder in Laravel 5
Related
I'm creating an application for a Symfony web project that I have made previously as a portable bundle. I've created a "media.html.twig" template and want it to be called by the render() helperfunction.
I've activated the bundle in /config/bundles.php with
App\OM\MediaManagerBundle\OMMediaManagerBundle::class => ['all' => true],
and followed the instructions on this site https://symfony.com/doc/current/bundles.html for setting up the Symfony-Bundle folder structure and the required files.
I've prefixed the path with "#MediaManager" as told by this site https://symfony.com/doc/4.1/bundles/best_practices.html#resources.
This is my Controller class:
<?php
namespace App\OM\MediaManagerBundle\Controller;
use App\OM\MediaManagerBundle\Form\ImageType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class MediaManagerController extends AbstractController
{
const TEMPLATE = "#MediaManager/MediaManager/media.html.twig";
/**
* #Route("/media/", name="om_mediamanager_media")
* */
public function index(Request $request)
{
$form = $this->createForm(ImageType::class);
return $this->render(self::TEMPLATE,
[
'form' => $form->createView(),
]);
}
}
This is how my folder structure looks like inside src:
https://i.imgur.com/9UomIEK.png
I hoped my form would be displayed but instead I got an error message saying:
There are no registered paths for namespace "MediaManager".
I know that it starts looking from the /templates folder so I tried setting the path to "../src/OM/MediaManagerBundle/Resources/views/MediaManager/media.html.twig" but got
Looks like you try to load a template outside configured directories (../src/OM/MediaManagerBundle/Resources/views/MediaManager/media.html.twig).
The code does work when I move the "media.html.twig" template inside the /templates folder but I expect it to ruin the portability of the bundle and therefor don't want to do that.
How do I access this template from inside my OMMediaManagerController class?
Am I even using Symfony Bundles the right way?
Is there any more documentation about Bundles in general? I found the ones provided by the official site to be very lacking.
I am trying to deep dive laravel concept. In the very first step i got stuck. Loading different classes and use them.
In the laravel routing (where you can register web routes for your application), there is no any use keyword used for using class and initiate Route class in web.php
Route::get('/home', 'HomeController#index')->name('home');
how Route::get run without using any class?
And when we go more deep using model class
namespace hosam\Http\Controllers\Auth;
use hosam\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Auth;
use Illuminate\Support\Facades\DB;
and we use Auth in aur code like this.
Auth::login($user);
from using use keyword does php load auth class in our code where we are using Use Auth?
All the route files under routes folder are loaded automatically by laravel. routes/web.php and laravel/api.php are assigned to middleware web and api respectively.
All the classes and namespaces in laravel are loaded from the composer autoloader.
These files are mapped in the RouteServiceProvider class under Provider folder. So that class use Route facade. As the web.php and api.php is not called directly so there is no need to initiate the class in the particiular file
//RouteServiceProvider.php
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
Routing
You can also make your Helper folder with HelperClass and run can be run from any where
make folder in app > Helpers folder.
make helper class Helpers.php
where you can write all your function like
function write_yourfunction(){
// your code
// return something
}
to load every where you can use service provider. Edit app > Providers > AppServiceProvider.php
public function register()
{
foreach (glob(app_path() . '/Helpers/*.php') as $filename) {
require_once($filename);
}
}
Now you can call write_yourfunction() from everywhere
I am using CAKEPHP for creating my Application apis,
Currently I have few controllers in `
app/controllers/{UsersController, AdminController, StoresController}
` etc
I am accessing my controllers as //users/
However I want to add versioning system in cakephp
Something like
<ip>/<foldername>/v1/users/<action>
<ip>/<foldername>/v2/users/<action>
I tried creating a Folder inside Controllers/ as Controllers/v1/UsersController.php
However I am unable to access it.
How can i configure my routes
I'm quite confused about CakePHP version you are using (you tagged cakephp-3.0, and folder structure you used in question resembles that of 2.x), but both 2.x and 3.x versions have common concept called Prefix Routing, which is what you should use here. Below example assumes CakePHP 3.x:
You need to create directories (v1, v2) inside your Controller directory. Next, create individual controllers in that folder, remembering that namespace should also reflect this structure:
<?php
namespace App\Controller\v1;
use App\Controller\AppController;
class UsersController extends AppController {
//...
}
Final step is to configure routing in your routes.php. Example below:
Router::prefix("v1", function (RouteBuilder $routes){
$routes->fallbacks(DashedRoute::class);
});
More info can be found here:
Prefix Routing
For CakePHP 2.x things are little different:
In v2.x, you should NOT create folders inside Controller directory. All prefixed and unprefixed functions will reside in single controllers, eg.
class UsersController extends AppController {
/**
* Unprefixed function
*/
public function login(){
}
/**
* Prefix v1
*/
public function v1_login(){
}
/**
* Prefix v2
*/
public function v2_login(){
}
}
Next, you need to configure these prefixes in your app/Config/core.php file like so:
Configure::write('Routing.prefixes', array('v1', 'v2'));
After this, you should be able to access your actions with v1 and v2 prefixes, eg yourapp/v1/users/login will map to UsersController::v1_login() and yourapp/v2/users/login to UsersController::v2_login().
More info in documentation for 2.x: Prefix Routing v2.x
I've had to try and work with an older package meant for php into my Laravel project.
I've added two custom classes, both are in the same folder "Classes" under the main "app" folder in my Laravel project.
One of these classes is recognized from a generated controller for my Laravel project. My paymentsController has use App\Classes\Quickbooks_Payments; in the top.
However, going to that Classes' file, I hit the following error through a route leading to my controller:
Class 'App\Classes\Quickbooks_Loader' not found
Now this is where this above is referenced in my paymentsController file:
<?php
namespace App\Classes\Quickbooks_Payments;
use App\Classes\Quickbooks_Loader;
/**
* QuickBooks Payments class
*
*/
/**
* Utilities class (for masking and some other misc things)
*/
QuickBooks_Loader::load('/QuickBooks/Utilities.php');
This last line is where the above error is referenced. And I do have both the Quickbooks_Loader.php and the Quickbooks_Payments.php in the same folder. My Quickbooks_Loader.php file starts off as such:
<?php
namespace App\Classes\QuickBooks_Loader;
So I know this is likely because of my inexperience with custom/imported classes. What am I doing wrong and how should I properly "import" these custom classes and have them recognized without any issues?
Change namespace in both classes you've shown to:
namespace App\Classes;
And run composer du
Also, make sure the class looks like this and not like you've shown (I'm not sure about did you cut something or not):
<?php
namespace App\Classes;
use App\Classes\Quickbooks_Loader;
class Quickbooks_Payments
{
public function __construct()
{
dd('It\'s working!');
}
}
I am trying to implement CodeIgniter style folder structure to use HMVC in Laravel. I am following this tutorial. However I am unable to route to the controller inside of the modules folder. My current Laravel folder structure is:
App
modules
model
view
controller
UsersController.php
I want to route to some function of UsersController.
you can simply use route as you do with normal controllers inside of default controller folder
Route::any('name', array('name' => 'SomeController#someMethod'));
but name of two controller shouldn't be same i.e controllers on controller folder and controller inside your module folder must have different name. For same name you can use namespace.