Set routes in Laravel 5.1 - php

I'm new in laravel, I wants to make separate folder for admin & front end. I've made folder like this
app/http/Controllers/Catalog/Common/home.php
app/http/Controllers/Admiin/Common/home.php
I wants to place common controllers in common folder like header, footer etc. In any another folder will be another related files like eg: category
app/http/Controllers/Catalog/Category/list.php
app/http/Controllers/Catalog/Category/product.php
app/http/Controllers/Admiin/Category/list.php
app/http/Controllers/Admiin/Category/categoryForm.php
But I'm stuck to make it working with routes, Should I use the same logic in Laravel or is there a better way to do it? Please suggest me how can I do it?
Here are the first four lines of my home controller
namespace LocalProject\Http\Controllers\Catalog\Common;
use Illuminate\Http\Request;
use LocalProject\Http\Requests;
use LocalProject\Http\Controllers\Controller;
I'd really appreciate any kind of help.

Routes work based off of namespaces, class names, and functions.
\Namespace\Classname#functionName
Folder structure shouldn't matter.
Edit for some more question specific answers:
You probably want to use something like this:
Route::get("home/index", [ "as" => "home.index", "uses" => "\LocalProject\Http\Controllers\Catalog\Common\HomeController" => "HomeController#index" ]);
You may also want to look into Route::group to help keep your namespaces more organized.

Related

Wrong controller is being used for edit route ( using Laravel resource helper )

I'm currently using laravel 5.4 and I have stumbled upon something I can't fix.
I'm currently trying to bind a route to a controller using the Laravel resource helper as such :
Route::resource('campaigns', 'CampaignsController');.
I correctly see my route being there when I do a PHP artisan:route list, I have all my CRUD endpoints tied to the appropriate controller function. Also, note that I'm currently doing that for all my route that need to be tied to a CRUD system ( what I'm working with is mostly form ) without any problem
With this being said, whenever I'm trying to edit a Campaign, I get an error : Class App\Http\Controllers\Ads\Campaigns does not exist
I do not know why it's trying to look for a Campaigns controller while I specify the CampaignsController controller. Everything is behaving correctly in campaigns route, except the edit one. Also, all my other routes have the same logic and never faced this problem.
Any idea why it is looking for the wrong Controller ?
Here's my namespace declaration and folder hierarchy, which is ok ( please note that the adsController has its routes declared the same way and is used the same way too )
here's my edit method
and here's the error
It's quite possible that you try to inject not existing class in your controller.
Take a look at controller constructor or edit route if you don't have something like this:
public function edit(Campaigns $campaigns)
{
}
and make sure you import Campaigns from valid namespace (probably it's not in App\Http\Controllers\Ads namespace.
If it doesn't help try to find in your app directory occurrences of Ads\Campaigns to see where it's used. Sometimes problem can be in completely different part of your application.
EDIT
Also make sure you didn't make any typo. In error you have Campaigns but your model is probably Campaign - is it possible that in one place you have extra s at the end?
Try with Route::resource('campaigns', 'Ads\CampaignsController'); in your web.php file

laravel routes, how to group similar in one file

I'm working with laravel(5.2), and there are a lot routes in my route file.
in fresh install I noticed that it was loading auth routes something like this.
Route::auth();
nothing else was there in routes.php file related to auth routes.
in my file, I've like this one
Route::get('color/event', 'ColorController#index');
Route::post('color/event', 'ColorController#post_message);
...
...
and many others, So I want to load all in laravel way, like Route::color(); and it should load all color related routes
Thanks for you time
you can try this
Route::resource('admin/settings','Admin\SettingsController');
and try this command
$ php artisan routes
Using Route::get(), Route::post() and similar functions is doing it the Laravel way - see the docs here https://laravel.com/docs/5.2/routing#basic-routing
Route::auth() is just a helper function introduced in Laravel 5.2 to keep all auth definitions together.
so, anyone if s/he is looking for same answer, I figured that out.
if you want something like Route::auth(); OR Route::color();//in my case or whatever you want to call it, you need to add custom function in your Router.php file. So solution will look like
//inside Router.php file
public function whatever(){
$this->get('app/', 'AppController#index');
$this->post('app/new', 'AppController#create');
}
and in your route.php file, you can do this.
Route::whatever();
But this is really dirty way to do that
so instead you can extend the base Router and register your router in bootstrap/app.php
$app->singleton('router', 'App\Your\Router');
so I community forces to use second approach.
for more details, have a look here.
Extending Router(laravel.io forum)
Extending default Laravel 5 Router
How to extend Router or Replace Customer Router class on Laravel5?
hope someone will find this useful
Thanks.

CakePHP - Change Controller name in URL

I have a legacy application built in CakePHP 2.2.3
One part of the application has controller file which has been named SymposiumsController.php. This resulted in URL's such as:
domain.com/symposiums
domain.com/symposiums/view/23
The problem is that 'symposiums' isn't a real (English language) word; it should be 'symposia'.
I want to rename my URL's so they are like this:
domain.com/symposia
domain.com/symposia/view/23
I tried to do this by editing app/Config/Routes.php to use this:
Router::connect('symposia/:action', array('controller' => 'symposiums'));
However all this does is redirects domain.com/symposia to domain.com/symposiums which therefore makes no difference to what the user sees in the URL.
To put it simply I don't want 'symposiums' exposed anywhere in my URLs. I want them all to use 'symposia' in it's place.
I read http://book.cakephp.org/2.0/en/development/routing.html but can't see how to do this. Does anyone have a solution? Surely I don't have to rename controllers/models and DB tables to do this?
I don't know if this makes a difference but I also have admin routing switched on so my SymposiumsController.php also has functions such as:
admin_add()
admin_delete()
admin_edit($id)
Any help is appreciated.
Here is the code for this specific redirection:
Router::connect('/:controller/:action/:id',
array('controller' => 'symposiums', 'action' => 'view', 1)
);
:controller => Give the name new name of controller e.g. symposia
:action => Give the name new name of action e.g. view
:id => Give the name new name of controller e.g. 23
But if you need to redirect more than one action then I suggest you to rename the controller.
Note: If you rename the controller or create new Routers then you would need to make sure in the all application modify the link to new controller name.
Source: Cakephp Router

How to use variable from routes.php in controller

In CakePHP have a bunch of unique URL names redirected in routes.php file.
Similar to this:
$beautiful_urls[0] = '/view/location-name/image-name.html';
Router::connect($beautiful_urls[0],
array('controller' => 'Foo','action' => 'bar',3,60));
I want to create facebook like buttons based on the beautified names. In order to do that I need the $beautiful_urls variable I use in the routes.php in the Foo controller.
How can I reach a variable in routes.php from a controller?
So far I tried to link it with App::use('routes','Config'); but it's not working. I also thought about sending the values as action parameters, but that doesn't seem like good practice... I know it's not a great idea to mix the config file with a controller's logic but I don't have any better idea so far.
I'm not cakephp user but simple search shows that there is class called ClassRegistry.
You can create class BeautifulUrls and store it there. According to docs it's singleton and It can be accessed from everywhere.
Also you can make BeautifulUrls implement ArrayAccess interface so you don't have to change your routes
I don't know if it's a good practice or not but my solution was to use the Configure class of CakePHP. It was straightforward to use and accessible everywhere in the code and the config files.
You can save key-value pairs with
Configure::write('key','value');
and read it again with
Configure::read('key');

Change Laravel's default (root) controller

In Laravel the default controller is the Home_Controller. However I have a controller called frontend. I want to use this instead of the home controller.
When I register a route like this:
Route::controller(Controller::detect());
then a request to /offer will be handled from within the home controller like home#offer. I want to use frontend#offer and access it from the site's root - not like /frontend/offer.
What should I do?
Thanks in advance.
Home_Controller is one of the hard-coded convention which exist in Laravel 3, however there are still ways to define routing to point the Frontend_Controller methods, my preference would be.
Route::any('/(index|offer|something)', function ($action)
{
return Controller::call("frontend#{$action}");
});
Limitation with this is that you need to define all supported "actions" method in Frontend_Controller.
My guess is that the only reason you think the Home_Controller is some sort of default is because you are using Controller::detect(); I really haven't seen anything in the documentation to make me think that the Home_Controller is anything special at all. In fact, it doesn't even look like it is routed to in the example documentation. Given that, my first suggestion would be to get rid of Controller::detect() and see if that fixes your problem.
Barring that, have you tried registering frontend as route named home? It appears that all URL::home() does is search for the 'Home' route, and then redirect to it. When using controller routing this can be done with something to the effect of.
Route::get('/',
array(
'as' => 'home',
'uses' => 'frontend#index'
)
);
Or is that not your desired effect? Do you want all routes which aren't otherwise found to be redirected to your frontend controller?
If you are concerned about your urls looking pretty, you can probably use some rewrite rules in your .htaccess file to make the whole process of routing to /frontend/index transparent you your users.
Add this to your routes.php :
Route::get('/', array('as' => 'any.route.name', 'uses' => 'frontend#offer'));
If you have any other / route, just remove it.

Categories