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
Related
I'm new to laravel so if my question is not up to the mark then forgive me but I googled it and not found answer as per my requirement.I follow https://laracasts.com/discuss/channels/laravel/setup-different-frontend-backend-endpoints-in-laravel-51 && https://laracasts.com/discuss/channels/general-discussion/splitting-admin-and-front-end
MY NEED :
I want 2 entry point for my laravel application. one is for front user and second for admin user.
I am using laravel latest version (5.3.26).
As we know public folder is the entry point for laravel application.
Ex : http://localhost/news_report_old/public/index.php
Using above URl we can enter into application. Right ?
Now I want to create new entry point for Admin So I thing I can copy public folder and rename it so my work is done but I missing something and I can't figure out what I am missing ?
EX : http://localhost/news_report_old/admin/index.php
So when I open above URL I want to open Admin side.
How can I achieve this ?
Thanks.
You, don't need to copy public folder. That is terrific idea. (Don't ever mess with core/structure framework code, unless you have no any other option)
You can distinguish front end and admin panel easily using router and middleware.
You should create a custom middelware:
https://laravel.com/docs/5.3/middleware
And routing can be like:
routes/web.php:
// List of all front end routes:
Route::get('/', 'ArticleController#index');
Route::get('/home', 'HomeController#index');
Route::get('/about', 'HomeController#about');
// List of all admin routes
Route::group(['prefix' => 'admin', 'middleware' => 'admin']], function () {
Route::resource('admin', 'AdminController')
});
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'm using the extension laravel-menu in my Laravel application.
This application contains multiple projects with multiple locations attached to each project.
Now I want to define a sidemenu where I can among other manage the locations.
The url of a project is
project/1
The url of the locations page of a project is
project/1/locations
How to setup this side menu in routes.php?
My routes.php code:
Route::resource('project', 'ProjectsController'));
Route::resource('project.locations', 'LocationsController');
Menu::make('sidemenu-project', function($menu) {
$menu->add('Locaties', array('route' => 'project.locations.index','{project?}'))->data('id',1); // this is not working
});
This is outputting the url /project/%7Bproject%7D/locations
Go to your terminal (Command Prompt) and run following command:
> php artisan routes
Then you'll see all the declared routes with their URL and corresponding route name and method name.
I'm very new to Laravel but the Routes page of documentation mentions you create a controller with parameters like this:
Route::get('user/{id}', function($id) { ... });
could you therefore define your route as
Route::get('project/{id}/locations', function($id) { ... });
I think you have this issue due to misconfiguring the routes. To achieve the route structure that you want, you should put your project/1/locations route definition above the first one. Consider your routes.php to be:
Route::resource('project/{project}/locations', ['as'=>'project.locations', 'uses'=> 'LocationsController']);
Route::resource('project', 'ProjectsController'));
I'm new to laravel.
This might be very simple but i was unable to find an example or documentation.
I need to redirect the user to an action in a controller that is in a sub folder.
Folder structure:
**app**
---**controllers**
------**Admin**
---------AdminHomeController.php (extends AdminController)
------AdminController.php
------BaseController.php
---**models**
---**views**
------**admin**
---------dashboard.php
------login.php
Routes.php
Route::get('/login', function()
{
return View::make('login');
});
Route::group(array('before' => 'auth'), function()
{
Route::resource('admin', 'AdminHomeController');
});
Route::post('/login', function()
{
Auth::attempt( ['email' => Input::get('email'), 'password' => Input::get('password')] );
**return Redirect::action('AdminHomeController#showAdminDashboard');**
});
After login i'm wanting to redirect to the action in AdminHomeController called "showAdminDashboard".
I know i could just load the view but i'm wanting to redirect.
My error is this - Unknown action [AdminHomeController#showAdminDashboard].
When you create new classes for things like controllers you'll need to dump your Composer autoload file again so that the classmap can be updated. If you open composer.json you should see a classmap key and the value will be an array of directories. One of the listed directories will be app/controllers.
Laravel doesn't know about your controller until your dump a new autoload. From a terminal simply run composer dump-autoload, it will take no longer then a couple of seconds.
Unless you namespace the controller, I think what you have to do is use an underscore to reference the controller in the route:
Route::group(array('before' => 'auth'), function()
{
Route::resource('admin', 'Admin_AdminHomeController');
});
Then make sure to rename the AdminHomeController class:
class Admin_AdminHomeController extends AdminController {
Leave the file name the same "AdminHomeController.php" and leave it inside the "Admin" folder. After that, run composer dump-autoload again, and I think you'll be working.
I have set up a resource controller using jefferyway's laravel4 generator as player.
So when i go to the url /players/show it shows me the show.blade.php. That's correct. But when I go to the /players/{whatever name field i can pass} it goes to the show.blade.php. No error thrown of httpnotfoundexception or anything.
These are the controller and routes file for the application.
http://paste.laravel.com/qwp
http://paste.laravel.com/qwq
That's the way it's supposed to work.
The show method on line 45 handles GET requests to /players/{anything}.
Jeffery Way has a really nice screencast series on Laravel 4, and he explains this in detail:
Resourceful Controllers: Part 1
Resourceful Controllers: Part 2
When you register a resource controller, it will create those routes for you :
GET /players players.index PlayerController#index
GET /players/create players.create PlayerController#create
POST /players players.store PlayerController#store
GET /players/{players} players.show PlayerController#show
GET /players/{players}/edit players.edit PlayerController#edit
PUT /players/{players} players.update PlayerController#update
PATCH /players/{players} PlayerController#update
DELETE /players/{players} players.destroy PlayerController#destroy
You can have this list with : php artisan routes
You can now see players.show will handle /players/*
Use Example:
Route::group(array('before' => 'auth'), function()
{
Route::get('/', function()
{
// Has Auth Filter
});
Route::get('user/profile', function()
{
// Has Auth Filter
});
});