I already have a working application and there is a PhoneController.php in controllers folder.
Now I want to add an api for my application so I added api\v1\PhoneController.php
But when I use routing this doesnt work as I want to:
Route::group(array('prefix' => 'api/v1'), function()
{
Route::get('test', 'PhoneController#index');
});
I tried adding 'namespace' => 'api\v1' or api\v1\PhoneController#index but this always picks the wrong PhoneController.
Are there anyways to get it work? I could rename the PhoneController.php but this could confuse me in future, so I am trying to avoid this solution
As described here http://daylerees.com/codebright/controllers
namespace Blog\Controller;
class Article extends \BaseController
{
public function showIndex()
{
return \View::make('index');
}
}
Then add route
Route::post('index', 'Blog\Controller\Article#showIndex');
Related
For some reason, which is probably my fault, Laravel thinks it should be looking for the class ApiController in path: 'App\Http\Controllers\App\Http\Controllers', so... it doubles, but I have no idea why.
It's a brand new Laravel 6 project, I've created the ApiController with the make:controller artisan command and added a function, like this:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ApiController extends Controller
{
public function base() {
return 'This is a test function';
}
}
Then I've added a route to the api routes like this:
use App\Http\Controllers\ApiController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::group(['prefix' => '/v1', 'as' => 'api'], function () {
Route::get('/base', ['uses' => ApiController::class . '#base'])->name('base');
});
As you can see, I've even 'imported' the controller, but it just can't find it.
That's it, no other files or changes to the project. Also tried clearing route cache and dump-autoload, but that did not change anything.
In my case problew was, in RouteServiceProvider, in using routes Namespace
protected $namespace = 'App\Http\Controllers';
In Laravel 8 namespace was commented out, i remove namespace from chain, because my web routes not fully moved to Laravel 8 syntax and i need this namespace.
Route::prefix('api')
->middleware('api')
-̶>̶n̶a̶m̶e̶s̶p̶a̶c̶e̶(̶$̶t̶h̶i̶s̶-̶>̶n̶a̶m̶e̶s̶p̶a̶c̶e̶)̶
->group(base_path('routes/admin-api.php'));
If you wanna ::class reference in the router, it should be done like this.
Route::group(['prefix' => '/v1', 'as' => 'api'], function () {
Route::get('base', [ApiController::class, 'base'])->name('base');
});
This should work:
Route::group(['prefix' => '/v1', 'as' => 'api'], function () {
Route::get('base', 'ApiController#base')->name('base');
});
No need to add the "use", since controllers are referenced from the App/Controllers namespace, as you can corroborate on the RouteServiceProvider.
The syntax of your route is a combination of "old syntax" vs "new syntax"
What you are trying to achieve is:
Route::get('/base', [ApiController::class, 'base'])->name('base');
Either remove this line:
use App\Http\Controllers\ApiController;
or add a \ to the start:
use \App\Http\Controllers\ApiController;
In my case (Laravel 8 project), I needed a separate route for destroy, because deleting didn't use html form, so my web.php file is like:
use App\Http\Controllers\LocationController;
...
Route::resource('/locations', LocationController::class);
Route::get('/locations/destroy/{location}', [LocationController::class, 'destroy']);
But in that case if I put use App\Http\Controllers\LocationController the first line (Route::resource...) fails, if I remove it then the second line fails. So I removed use line and added App\Http\Controllers into the second line:
Route::resource('/locations', LocationController::class);
Route::get('/locations/destroy/{location}', [App\Http\Controllers\LocationController::class, 'destroy']);
So obviously Laravel doesn't automatically add App\Http\Controllers in the second form of Route.
I got this error when in resource controller description me pasted one from fresh project:
Route::resources([
'my_url' => LisseyDoruHisobotController:class,
..., //other controllers
]);
as being a recommended signature in Laravel 8 , but am currently busy on 7 or 6 version, where should be:
Route::resources([
'my_url' => 'path\to\LisseyDoruHisobotController',
..., //other controllers
]);
otherwize it will show doubled path
Laravel Target class [App\Http\Controllers\App\Http\Controllers\ ] does not exist
Couldn't think of a more descriptive title.
Here's the dilemma, I'm wondering if anybody has a good approach to this or if I've overlooked something obvious?
Picture these two routes:
/category/product
/parent/child
The first route deals with products in a shop.
The second route deals with regular pages in the CMS.
How do I distinguish between these two routes in my application? I don't want to append something before each route (ie /shop/category/product, /page/parent/child) if possible.
Any ideas?
in your routes.php file put this code
Route::get('/{action?}/{name?}',[
'uses' =>'NiceActionController#getNiceAction',
'as' =>'niceaction'
]);
then go to controller folder and create a file NiceActionController then :
<?php
namespace App\Http\Controllers;
use \Illuminate\Http\Request;
class NiceActionController extends Controller
{
public function getNiceAction($action,$name=null)
{
return view('actions.'.$action , ['name' => $name]) ;
}
}
Should be able to just stack the routes:
Route::get('category/{product}', 'Controller#function');
Route::get('{parent}/{child}', 'Controller#function');
You can group your routes.
Route::group(['prefix' => 'parent'], function () {
Route::get('child', 'Controller1#action1');
Route::get('anotherchild', 'Controller2#action2');
});
This gives you routes /parent/child and /parent/anotherchild.
There are more options, read more at documentation
I cloned this todstoychev/Laravel5Starter from Github and installed it.
After creating this StaticPagesController controller and updating my routes.php file. The controller does not seem to work. For some reason i keep getting the following error.
ReflectionException in ControllerInspector.php line 32:
Class App\Http\Controllers\StaticPagesController#faq does not exist
My routes.php file
<?php
// Admin routes
Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function () {
Route::controller('permissions', 'AdminPermissionsController');
Route::controller('settings', 'AdminSettingsController');
Route::controller('roles', 'AdminRolesController');
Route::controller('users', 'AdminUsersController');
Route::controller('/', 'AdminController');
});
// Public and user routes
Route::controller('contacts', 'ContactsController');
Route::controller('users', 'UsersController');
Route::controller('/', 'IndexController');
Route::controller('faq', 'StaticPagesController#faq');
My StaticPagesController.php file
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class StaticPagesController extends Controller
{
public function faq(){
return 'this is faq page';
}
}
I have tried composer update, php artisan acl:update, composer dumpautoload to no avail.
Please help me. Thanks
With this line:
Route::controller('faq', 'StaticPagesController#faq');
You are telling Laravel that the controller for faq shoule be StaticPagesController#faq. The Route::controller method sets an entire controller for a route, it does not specify a method to be used on that route, Laravel handles this internally. Take a look at your error to prove my point:
Class App\Http\Controllers\StaticPagesController#faq does not exist
It is looking for class StaticPagesController#faq not StaticPagesController as you are intending.
Unless you are building an API using REST, you should not use the controller method and instead specify your routes explicitly, i.e.
Route::get('faq', 'StaticPagesController#faq');
This will use the faq method on your controller when the user makes a GET request to the URI faq. If you insist on using the controller method, then remove the #faq from the second argument and you will be good, although I'm pretty sure Laravel expects the methods index, show, create, etc to be in your controller. I suggest taking a look at the Laravel 5 Fundamentals video course to help you get a better understanding.
I am using code that is below for admin routing in laravel.
Route::group(['prefix' => 'admin'], function() {
Route::get('/', 'Admin\AdminController#home');
Route::get('/users/userList', 'Admin\UserController#userList');
Route::get('/users/detail', 'Admin\UserController#detail');
Route::get('/posts/view', 'Admin\PostController#view');
Route::get('/posts/edit', 'Admin\PostController#edit');
Route::get('/posts/add', 'Admin\PostController#add');
});
This is working fine for me. But when I add new functions in code for that I have to write routing in routes file. For example: If I want to add edit functionality in users controller, for that I have to add new route like .
Route::get('/users/edit', 'Admin\UserController#edit');
So I have to add routing for each function.
I want to know How to use wild card for admin routing so that I have to write routing only for controller not for each function for example.
Route::group(['prefix' => 'admin'], function() {
Route::get('/', 'Admin\AdminController#home');
Route::get('/users/:any', 'Admin\UserController#:any');
Route::get('/posts/:any', 'Admin\PostsController#:any');
});
wild card replace the function name, and auto ridirect to that function.
You could use implicit controllers that will do what you need.
First declare a route for your implicit controller
Route::controller('users', 'UserController');
Then, on your controller, you have to follow a convention for naming your routes with HTTP verbs used to access them (get for GET, post for POST, any for both)
class UserController extends Controller {
public function getIndex()
{
//
}
public function postProfile()
{
//
}
public function anyLogin()
{
//
}
}
A note about composed method name from documentation
If your controller action contains multiple words, you may access the action using "dash" syntax in the URI. For example, the following controller action on our UserController would respond to the users/admin-profile URI:
public function getAdminProfile() {}
How can I create admin specific routes in Laravel 4 (Restfull Controllers):
/admin/users (get - /admin/users/index)
/admin/users/create (get)
/admin/users/store (post)
I want to know:
What Files and where I need create theam
How I need create the route
In Laravel 4 you can now use prefix:
Route::group(['prefix' => 'admin'], function() {
Route::get('/', 'AdminController#home');
Route::get('posts', 'AdminController#showPosts');
Route::get('another', function() {
return 'Another routing';
});
Route::get('foo', function() {
return Response::make('BARRRRR', 200);
});
Route::get('bazz', function() {
return View::make('bazztemplate');
});
});
For your subfolders, as I answer here "route-to-controller-in-subfolder-not-working-in-laravel-4", seems to have no "friendly" solution in this laravel 4 beta.
#Aran, if you make it working fine, please add an code sample of your controller, route, and composer.json files :
Route::resource('admin/users', 'admin.Users');
or
Route::resource('admin', 'admin.Users');
thanks
Really useful tool that you can use is the artisan CLI.
Using this you'll be able to generate the needed function file with all the required routes for it to become RESTful.
php artisan controller:make users
Would generate the function file for you. Then in your routes.php file you can simply add
Route::resource('users', 'Users');
This'll setup all the necessary routes.
For more information on this you should read the documentation at.
http://four.laravel.com/docs/routing#resource-controllers
http://four.laravel.com/docs/artisan
Edit:
To make this admin specific, simple alter the code like follows and move the controller to a admin folder inside the controllers folder.
Route::resource('admin/users', 'admin.Users');
The first paramater is the route, the second is the controller filename/folder.
In Laravel if you placed a controller inside a folder, to specific it in a route or URL you'd use the a dot for folders.
You can then expand on this and add Authentication using Route Filters and specifically the code found "Pattern Based Filters" found on the page below.
http://four.laravel.com/docs/routing#route-filters
Laravel 4 - Add Admin Controller Easily
This was driving me insane for ages, but i worked it out.
routes.php
Route::resource('admin', 'Admin_HomeController#showIndex');
/controllers/Admin/HomeController.php
Notice the folder name Admin must be captital 'A'
<?php
class Admin_HomeController extends Controller {
public function showIndex() {
return 'Yes it works!';
}
}
Alternatively you can use the group method
Route::group(array('prefix' => 'admin'), function() {
Route::get('/', 'Admin_HomeController#showIndex');
});
Thanks
Daniel