How can I make a namespace for Laravel controller? - laravel-4

I'm trying to make controller SecondController with namespace Admin to be Admin\SecondController
this controller should be saved in "controllers\admin" folder with lowercase (a)
when trying to generate controller using
artisan controller:make "Admin\SecondController"
it save the controller in folder Admin with upper case (A)
so the controller doesn't work as i use it in route namespace
is there any way to make controller SecondController with uppercase "A" namespace "Admin" and put that file inside lowercase "a" folder "controllers\admin" with lowercase ?

Related

Using multiple Routes and Controllers on single blade file

I have created multiple controller and routes but they are working 1 at a time, I have to disable the other and change the code of my blade file or use different blade file for them but is there an easy way to use it.
The routes are
Route::get('/students/{alphabet}', 'PostController#showByAlphabet');
Route::get('/students/{name}', 'PostController#showByName');
Route::get('/students/{class}', 'PostController#showByClass');
I do not want to create different blade files like
http://example.com/students/alphabet/a
http://example.com/students/name/nadia
http://example.com/students/class/b_com
but like this
http://example.com/students/a
http://example.com/students/nadia
http://example.com/students/b_com
is it possible?
All controllers show different data.
1. Alphabet show list of students starting with same initial.
2. Name shows profile data of the student.
3. Class shows list of students in that subject class.
Since you have a wildcard at the end of your routes the first one will always trigger. So make sure you have individual routes for the controller functions. You can still use the same blade file in the controller.
Route::get('/students/alphabet/{alphabet}', 'PostController#showByAlphabet');
Route::get('/students/name/{name}', 'PostController#showByName');
Route::get('/students/class/{class}', 'PostController#showByClass');
If you have the same route with a parameter there is no way for the router to know if the characters you're sending in are alphabet, name or class.

How to route to a custom function in controller

I've create a function in UsersController named changepassword and also added following code to routes/web.php
Route::any("/users/changepassword","UsersController#changepassword")->name("users.changepassword");
But when I trying to access /users/changepassword go to function show rather than changepassword.

LARAVEL ROUTES not accept

and it works:
im add route and have view:
and errors:
wtf??? only copy paste, and rename...
Check layouts/app.blade.php There's something like route('qwe') and this route doesn't exist at all. So delete it from the layout and it should work.
Update
You're calling route('kwe') but you didn't name the route in the file. so add a name method to your route like the following.
Route::get('kwe', function(){
return view('kwe');
})->name('kwe');
Update 2
route helper's parameter is the route name not its path. So you need to name the route before calling it using route helper.
If you wanna use the path itself instead you can use url helper.

Laravel-4: Difference between RESTful Controllers and Resource Controllers in Laravel

Someone can please explain what is the difference between RESTful Controllers and Resource Controllers in Laravel ? I also have some Questions-
when should I use RESTful Controllers and when Resource Controllers?
Is there any naming convention Of Controller action for RESTful Controllers and Resource Controllers ?
If I use RESTful Controllers how could I define route for our controller ?
For building API which Controller Method is the best ?
Laravel Resource Controllers are defined as Route::controller('users', 'UserController'); while Restful Controllers are defined as Route::resource('photo', 'PhotoController');.
A restful controller follows the standard blueprint for a restful resource which mainly consists of:
GET /resource index resource.index
GET /resource/create create resource.create
POST /resource store resource.store
GET /resource/{resource} show resource.show
GET /resource/{resource}/edit edit resource.edit
PUT/PATCH /resource/{resource} update resource.update
DELETE /resource/{resource} destroy resource.destroy
While the resource controller isn't opinionated like the restful controller. It allows you to create methods directly from you controller and it all gets automatically mapped to your routes:
public function getIndex()
{
// Route::get('/', 'Controller#getIndex');
}
public function postProfile()
{
// Route::post('/profile', 'Controller#postProfile');
}
Will automatically have the routes like Route::post('/profile', 'Controller#postProfile'); without explicitly defining it on the routes, much more of a helper if you will to avoid very long route files.
Doing php artisan routes will show you all your routes. You can test stuff out and use that command to see what routes gets automatically generated.
They are different concepts. In laravel, a resource controller defines all the default routes for a given named resource to follow REST principles.
So when you define a resource in your routes.php like:
Route::resource('users', 'UsersController');
The only thing Laravel does is define for you this routes:
Verb Path Action Route Name
GET /resource index resource.index
GET /resource/create create resource.create
POST /resource store resource.store
GET /resource/{resource} show resource.show
GET /resource/{resource}/edit edit resource.edit
PUT/PATCH /resource/{resource} update resource.update
DELETE /resource/{resource} destroy resource.destroy
And expects that you define those methods on your controller. You can also use only/except clauses to remove unneeded routes:
Route::resource('user', 'UserController', ['except' => ['destroy']]);
More on this on Laravel's documentation.
It's just a distinction about the routing declaration. Instead of using one of those, manually define all of your routes.
Route::get(...);
Route::post(...);
Route::put(...);
Route::delete(...);
Route::patch(...);
It makes your routes file authoritative, easy to understand, and less buggy.
The documentation currently shows RESTful and Resource controllers to refer to the same thing.
Route::resource('resource', 'ResourceController');
It defines the routes for the following http request verbs mapped to the URI, controller action, and route. This allows you to use the predefined route names to connect to predefined controller actions and will map resource_id to {resource} as shown.
Verb URI Action Route Name
GET /resource/index.blade.php index resource
GET /resource/create.blade.php create resource.create
POST /resource store resource.store
GET /resource/{resource}/show.blade.php show resource.show
GET /resource/{resource}/edit.blade.php edit resource.edit
PUT/PATCH update resource.update
DELETE destroy resource.destroy
The term Implicit Controller seems to be the term to specify the use of
Route::controller('resource', 'ResourceController');
which will magically connect all routes to to ResourceController so that the http request verb (get/post) is prefixed in the function name used in the controller. This maps any URI to the controller action (function) with (get/put) in front but does not map resource_id to {resource} or route names.
class UserController extends BaseController {
public function getIndex()
{
//
}
public function postProfile()
{
//
}
public function anyLogin()
{
//
}
}
maps to
Verb URI Action Route Name
GET /index getIndex
POST /profile postProfile
GET /login anyLogin
POST /login anyLogin
DELETE /login anyLogin
It's up to you to decide which method to use if any for routing. There is some debate as to what is useful and if routing is even worth the confusion it can cause.
RESTful Resource Controllers
Resource controllers make it easier to build RESTful controllers around resources. For example, you may wish to create a controller that manages "photos" stored by your application. Using the controller:make command via the Artisan CLI and the Route::resource method, we can quickly create such a controller.
To create the controller via the command line, execute the following command:
php artisan controller:make PhotoController
Now we can register a resourceful route to the controller:
Route::resource('photo', 'PhotoController');
This single route declaration creates multiple routes to handle a variety of RESTful actions on the photo resource. Likewise, the generated controller will already have stubbed methods for each of these actions with notes informing you which URIs and verbs they handle.
Actions Handled By Resource Controller
http://laravel.com/docs/5.0/controllers#restful-resource-controllers

How can I prevent duplicate content when re-routing pages in CodeIgniter?

Say I have a controller, "Articles" but I want it to appear as a sub-folder (e.g. "blog/articles"), I can add a route like this:
$route['blog/articles'] = 'articles';
$route['blog/articles/(:any)'] = 'articles/$1';
This works fine, the only problem now is that example.com/articles and example.com/blog/articles both use the Articles controller and thus resolve to the same content. Is there a way to prevent this?
To add a little more clarity in case people aren't understanding:
In this example, I don't have a 'blog' controller, but I want 'articles' etc to appear to be in that subfolder (it's an organization thing).
I could have a blog controller with an 'articles' function, but I'm likely to have a bunch of 'subcontrollers' and want to separate the functionality (otherwise I could end up with 30+ functions for separate entities in the blog controller).
I want example.com/articles to return a 404 since that is not the correct URL, example.com/blog/articles is.
Shove this in your controller:
function __construct()
{
parent::Controller();
$this->uri->uri_segment(1) == 'blog' OR show_404();
}
You can use subfolders in Codeigniter controllers, so in CI, the following directory structure works:
application/controllers/blog/articles.php and is then accessed at
http://example.com/blog/articles/*.
If, for some reason, you're set on routing instead of accessing the controllers in folders (you want to have a blog controller, for example, and don't want to route to it), you can do as suggested above and add the test for 'blog' to the constructor.
If you're in PHP5, you can use the constructor function like this:
function __construct()
{
parent::Controller();
$this->uri->uri_segment(1) == 'blog' OR redirect('/blog/articles');
}
or, in PHP4, like this:
function Articles()
{
parent::Controller();
$this->uri->uri_segment(1) == 'blog' OR redirect('/blog/articles');
}
I would suggest using redirect('blog/articles') instead of show_404(), though, so that you're directing users who hit /articles to the correct location, instead of just showing them a 404 page.
Routing there does not mean it will use a different controller, it just creates alias url segment to same controller. The way will be to create another controller if you are looking to use a different controller for those url segments.
If both /blog/ and /articles/ use the same controller, you can reroute one of them to a different one by just adding a new rule in your routes file.

Resources