Laravel controller define with mask - php

I have PagesController defined in my routes file:
Route::controller('/', 'PagesController');
But i use some more routes like:
Route::get('/admin', function()
{
....some code here
});
My second route doesn't work, because all other routes try to find functions in PagesController. I can change my controller to:
Route::controller('pages', 'PagesController');
But then in my home page, all links will be like www.test.com/pages/..., but i don't need that 'pages' in there. How to define my controller with mask or something like that?

Laravel allows you to easily define a single route to handle every action in a controller using simple, REST naming conventions. First, define the route using the Route::controller method:
Route::controller('pages', 'PagesController')
This is a single route to define all actions in a controller using REST naming conventions therefore you get the /pages.
For the root of your app you need to specify the method that you want to call within your PagesController.
Example:
Route::get('/', array('as' => 'home', 'uses' => 'PagesController#getIndex'));
Place this line at the top of your routes in the routes file.

Change the order of your route definition to this:
Route::get('/admin', function()
{
....some code here
});
Route::controller('/', 'PagesController');
It will now look for /admin first, and if it cant find it, it when they go to your other routes...

Try changing Route::controller({same content as the question}) to Route::resource({same content as the question})

Related

Laravel: How to fix routing the index page is not defined, but the routing declared already on the controller?

I got confuse and I got an error, why does my routing process not work, The error gives me Route [index] not defined, but on other hand I already defined the index to HomeController, take a look at my process that I did,
Note: I used laravel version: 5.8*
I create a index.blade.php
Add the routes to the web.php and I used this code
`Route::get('/index', 'HomeController#index');
I add the public function index to the HomeController
Web.php
Route::get('/index', 'HomeController#index');
HomeController
public function index()
{
return view('index');
}
My URL:
Error:
The problem might be in your index view.
Looks like you are trying to access route using route name and you have not defined the route name for index route.
So in web.php add ->name('index')
Route::get('/index', 'HomeController#index')->name('index');
You have to provide name of the route in your routes.
Route::get('/index', 'HomeController#index')->name('index');
You can also use the below syntax
Route::get('/index', [
'as' => 'index',
'uses' => 'HomeController#index'
]);
for more information please have a look at the docs
https://laravel.com/docs/5.7/routing#named-routes
Try This
if you use route this way
Route::get('/index', 'HomeController#index');
//then your url will be
URL/index
OR use this way
Route::get('/', 'HomeController#index');
//then your url will be
URL
Try with localhost/folder_name/public/index if this works for you then probably problem is with virtual host creation.
Somewhere in your view you are using {{route('index')}}.
Add ->name('index') in the end of your route.
Route::get('/index', 'HomeController#index')->name('index');
Hope this will help.

How to handle root route with parameter in laravel?

So I've got the current route setup:
Route::get('/{id}', 'MainController#index');
This is for passing in a id through / but I would also like:
Route::get('/admin', 'AdminController#index');
but it keeps handling admin as a URL parameter rather than it's own route, is there a way of distinguishing between the two?
You need to move this route to the end of the routes file to make all other routes similar to '/admin' work:
Route::get('/{id}', 'MainController#index');

Change laravel welcome file name

I changed the files path after i installed laravel framework like this:
from:
resources/views/welcome.bandle.php
to
resources/views/admin/index.php
and the routes file to:
Route::get('/admin', function () {
return view('admin/index');
});
the url is working
but all the larvael render not working
like this:
what i need to do?
tnx a lot.
You need to add the .blade.php extension to the files you want to parse using "Blade Engine", that will remove all the tags you have within curly braces.
Next, you need to write your route like this:
Route::get('/admin', function(){
return view('admin.index');
})->name('admin.index')->middleware('auth');
It is a good convention naming the routes for easy access across the application, that way you can simply reference it in the blade views like this:
Admin page
That way you will have the dynamic route no matter from where in the file structure you call it.
Or you can also use your Controller to display such view. By this you'll write your routes more cleaner. Let's say we have an AdminController that handles all admin processes and functions. Put your dashboard.blade.php inside views/admin directory.
The route:
Route::get('/admin', 'AdminController#index');
The controller:
class AdminController extends Controller
{
public function index()
{
return view('admin.dashboard'); // in views->admin->dashboard.blade.php
//add some data here
}
}
Just keep 'blade' in view file name if you don't plan to use controller, e.g.:
resources/views/admin/index.blade.php

Routing in laravel framework

I using the laravel framework of php for development.I done these following
steps
I define Route::resource('users', 'UsersController'); in route file and then define Route::get('user/pingme', 'UserController#pingme');
When i make a get call to pingme function, it was not working .I was getting the response code is 200 but code inside that pingme function was not working and i do not know why.
then i changed it to Route::post('user/pingme', 'UserController#pingme'); it was working fine as needed.
then what i did is, removed Route::resource('users', 'UsersController'); and make again get route to ping me function and make get call and it starts working fine .
so this is any bug in framework(rare thing) or i am missing something(probably yes)? Help me out....
Route file works as follows:-
if you have wrote a mapping for controller only, then it needs to come at the bottom of all other route mapping otherwise your program controller will pick route from user controller only and will redirect to UserController.
so the right order of all routes is:-
Route::get('user/pingme', 'UserController#pingme');
Route::post('user/logout', 'UserController#logout')->before('auth');
Route::resource('user', 'UserController');
OR
Route::post('user/logout', 'UserController#logout')->before('auth');
Route::get('user/pingme', 'UserController#pingme');
Route::resource('user', 'UserController');
In your route file, the order of the routes needs to be as follows:
Route::get('user/pingme', 'UserController#pingme');
Route::post('user/logout', 'UserController#logout')->before('auth');
Route::resource('user', 'UserController');
If Route::resource('user', 'UserController') comes before the other routes, the GET request to user/pingme will be handled by show method inside of UserController, because it is how Resourceful Controllers work. So, the Route::resource for user needs to come after all other routes with user/ prefix.

Admin Routes (or Prefix Routes) in Laravel 4

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

Categories