As well to use controller inside another controller Laravel? - laravel

I have two entities: users, announcements
Eaach user can publish the announcements.
I creted controller AnnouncemetController, where there is method class: my(), that returns notes for current user.
Also I have controller ProfileController that represent current profile user, where I need to show all announcements of user.
For this I tried to reuse controller AnnouncemetController inside ProfileController and call public method my().
use App\Http\Controllers\AnnouncementController;
class ProfileController extends Controller
{
$my = new AnnouncementController();
$my->my();
}
Is it well to use such?

A Laravel controller maps a uri to an action. In your example, you are using a controller to access data, so this is not the "right thing to do".
Instead use model methods to access the data.

Related

How to call method dynamically on routes on Laravel?

I'm a newbie with this and I need some help.
I'm developing some kind of music library and let's say I don't want to make a route for each artist so I have made this one:
Route::get('/{artist_name}', 'Artist_controller#{artist_name}');
I get the value of {artist_name} from my view and the route works, for instance, the artist_name may be John and the url generated is localhost:8000/John. But when it comes to look for the class in the controller it doesn't work. I have a class named John in my controller, but I keep getting this error when I try to access:
BadMethodCallException
Method [{artist_name}] does not exist.
So I guess the route isn't taking the value of {artist_name}. What I intend is the route to be processed like:
Route::get('/John', 'Artist_controller#John');
But as I said, I don't want to create a specific route for an artist.
I'd appreciate any kind of help. Thank You
There is no need to create a dynamic method for each artist. You could have one generic method in your controller that handles retrieving the proper artist information from the database and pass it to the view.
routes file:
Route::get('artists/{artist_id}', 'ArtistsController#show');
ArtistsController.php
class ArtistsController extends Controller
{
public function show($artist_id)
{
$artist = Artists::find($artist_id);
return view('artists.show', ['artist' => $artist]);
}
}
So if the user hits the following URL http://localhost/artists/4 the artist id of 4 will be passed to the show method and it will dynamically looks for an artist with that ID and pass an object of artist to your view.
Of course you are not limited to IDs in your URLs. You can use the name if it was unique and your code will be as the following.
routes file:
Route::get('artists/{artist_name}', 'ArtistsController#show');
ArtistsController.php
class ArtistsController extends Controller
{
public function show($artist_name)
{
$artist = Artist::where('name', $artist_name);
return view('artists.show', ['artist' => $artist]);
}
}
I suggest you read this documentation for more information about routing.
You can not have dynamic method (controller action) in a controller class. Instead you should define a method and pass the route parameter to that action.
In your route (web.php) file:
Route::get('/{artist_name}', 'ArtistController#artist');
then in ArtistController.php:
public function artist ($artist_name) {
// do stuff based on $artist_name
}
To get more info read these 2 documentation pages. Controller and Routing.

Create route for every controller of index method in Laravel

I'm developing a school management system in laravel. I have many controllers like
controller staff in method index
class controllerstaff extends controller {
public function index{
//here process of staff data
}
}
//this controller have `Route::get('/', 'controllerstaff#index');
and other controller
class controllerstudent extends controller {
public function index{
//here process of student data
}
}
//this controller have Route::get('/', 'controllerstudent#index');
As above does not work properly.
Any one can tell me how to create route for every controller of index method. If we crate many route file then how operate it and how access in controller and form action
You cannot create same urls for each route. For each route you need to have different url, for example:
Route::get('/staff', 'controllerstaff#index');
Route::get('/students', 'controllerstudent#index');
You should also name your controllers rather StudentController and not controllerstudent. You might also consider looking at Routing documentation before creating code - I believe it might be the right way ;)

Laravel - Controller on Controller Dependecy

I have a Post model and a User model. Furthermore, I have a PostController and a UserController. Each post view has to be accompanied with its user view that displays the user information. Also, user has its own route and UserController allows each user view to be individually.
I would like to make it mandatory in my code to display the user view prior to calling the post view. In other words, PostController has a dependency on UserController. How can I achieve this?
You can achieve your goal in two ways.
First, you can use the view's include('view_file_name') to insert that view in the PostController's view. but then you have to initialise the variables in the PostController and pass it to the view.
Second, You can extends the UserController. if you wanted use the variables and functions of UserController in the PostController.
class PostController extends UserController {
}
Remember __construct of the UserController will be called when you call any function of PostController if you extends the controller.

Pass data from routes.php to a controller in Laravel

I am attempting to create a route in Laravel for a dynamic URL to load a particular controller action. I am able to get it to route to a controller using the following code:
Route::get('/something.html', array('uses' => 'MyController#getView'));
What I am having trouble figuring out is how I can pass a variable from this route to the controller. In this case I would like to pass along an id value to the controller action.
Is this possible in Laravel? Is there another way to do this?
You are not giving us enough information, so you need to ask yourself two basic questions: where this information coming from? Can you have access to this information inside your controller without passing it via the routes.php file?
If you are about to produce this information somehow in your ´routes.php´ file:
$information = WhateverService::getInformation();
You cannot pass it here to your controller, because your controller is not really being fired in this file, this is just a list of available routes, wich may or may not be hit at some point. When a route is hit, Laravel will fire the route via another internal service.
But you probably will be able to use the very same line of code in your controller:
class MyController extends BaseController {
function getView()
{
$information = WhateverService::getInformation();
return View::make('myview')->with(compact('information'));
}
}
In MVC, Controllers are meant to receive HTTP requests and produce information via Models (or services or repositores) to pass to your Views, which can produce new web pages.
If this information is something you have in your page and you want to sneak it to your something.html route, use a POST method instead of GET:
Route::post('/something.html', array('uses' => 'MyController#getView'));
And inside your controller receive that information via:
class MyController extends BaseController {
function getView()
{
$information = Input::get('information');
return View::make('myview')->with(compact('information'));
}
}

Action parameters & Doctrine entities in Symfony 2

We're running a project built on top of Zend Framework 1.x, and are considering moving to Symfony 2. We have a domain model mapped with Doctrine 2.
Our (custom built) base controller class extends Zend_Controller_Action to provide a very convenient feature, inspired from Flow3:
Let's say I have this controller:
class UserController extends BaseController
{
public function editAction(User $user)
{
// ...
}
}
If I load this URL:
/user/edit?user=123
The base controller will automatically load the User entity with identity 123, and pass it as a parameter to the editAction() method.
If the user parameter is omitted, or if no User with this identity exists, an exception is thrown.
Is there such an implementation for Symfony 2, or is it possible to implement it, and how?
The #ParamConverter annotation from SensioFrameworkExtraBundle does exactly that. If you're using the Symfony Standard distribution, you get it out of the box.

Resources