I have a controller name CodeBridgeController and have a model table name CodeBridgeTable.
When I call CodeBridge table in action like following:
$this->CodeBridge->find('all')->toArray();
This generate Error: Call to a member function find() on a non-object
File D:\php\htdocs\cake3\erp\src\Controller\CodeBridgeController.php
Line: 25
I think this is silly matter but i could not find any wrong in the code why table is not loaded in this context.
When i call this way it works.
$codeBridgeTable = TableRegistry::get('CodeBridge');
$results = $codeBridgeTable->find('all')->hydrate(false)->toArray();
What may create the problem, expert suggestion needed.
Finally, I found where the problem lies.
In controller action, I generating a url like following:
Router::url(array('controller' =>'codebridge','action'=>'edit')); which generate url /cake3/erp/codebridge/edit
After changing the controller name in camel case Router::url(array('controller' =>'CodeBridge','action'=>'edit')); which generate url '/cake3/erp/code-bridge/edit`
This solve the problem
To resolve this, just load the model prior to trying to use it:
$this->loadModel("CodeBridge");
$this->CodeBridge->find('all')->toArray();
Related
I am trying to make commenting on my site modular, so I have attempted to use view::composer to run the queries that are required to populate the comment data whenever the comment view is called, then my plan was to include the comment view in any place i needed comments, and then connect it to the appropriate comment table in my database.
The functions I have made for commenting fit each of the comment tables I've created and I've made the view composer work, the only thing is now since the function to get all the comment data is being called by the view composer when the comment view is being set up, that function no longer has access to the id that was being passed to it ( normally it is in the route like: Route::get('/{id}', 'controller#method');).
I have been searching for hours trying to figure out how to pass the variable in, but with no luck.. Maybe I'm missing some really basic detail, Idk, but I can't figure it out and haven't found anything that solves the problem, please help..
It seems like it should be so simple, I've tried doing:
$var = Route::get('/{id}', function($id){ return $id; }
Which i thought might do it, but the result was an error saying whatever the response was couldn't be converted to string.
Option 1: Access the route parameter
View::composer('comment-view', function($view){
$id = Route::current()->getParameter('id');
});
Option 2: Use the view to access the id
In your controller: return View::make('view-name')->with('commentId', $id);
View::composer('comment-view', function($view){
$id = $view->commentId;
});
I am trying to figure out how to access two (or more) parameters passed to a Laravel controller. I know how to create the route, and the URL is created correctly, but then I can only access the first passed parameter in my controller.
Route:
Route::get('managers/{id}/{parameter2}', array('as'=>'dosomething', 'uses'=> 'ManagersController#dosomething'));
where the first parameter is obviously the $id for managers, and the second parameters is to be processed by the controller.
View:
Do Something
generates the URL:
http://domain/managers/1/2
where 1 is easily accessed as the $id for managers, but when I try to access the 2nd parameter "2" using $parameter2, e.g. using a simple return: "id=$id and parameter2=$parameter2" statement, I get an "unidentified variable: $parameter2" error.
What am I doing wrong?
Is there a better way to pass multiple parameters? I'm especially asking the "better way?" question because what I want to do is use the 2nd parameter to change a value in a database table, and using a 'get' method, somebody could change the parameter value in the URL and therefore cause mischief. Must I use a 'post' method? I'd love to be able to use a link, since that works much better with the design of my application.
Thanks!
I was asked to include the controller, which I'm happy to do. Initially, just for testing, as I mentioned, my controller was a simple return to display the values of the two passed parameters. But here is what I want to be able to do, including the actual name of the function ("update_group" rather than "dosomething") --
ManagersController:
public function update_group($id)
{
DB::table('groups')->where('id','=',$parameter2)->update(array('manager_id'=>$id));
return Redirect::route('managers.show', array('id'=>$id));
}
The update table works perfectly if I replace $parameter2 with an actual value, so that syntax is fine. The issue is that Laravel says that $parameter2 is an undefined variable, despite the fact that the URL contains the value of $parameter2 as you can see above.
And since it occurs to me that the answer to this may involve adding a function to the Manager model, here is the current
Manager.php
class Manager extends Eloquent {
protected $table = 'managers'; ... (mutator and error functions)
}
Just change
public function update_group($id)
to
public function update_group($id, $parameter2)
All looks ok in your route. Seeing the controller code would help, but likely, you may not have a second parameter in your controller's dosomething() method.
public function dosomething($id, $parameter2){
var_dump($id).'<br />';
var_dump($paremter2);
}
If that isn't the case, you can try dumping it from the route's callback to further diagnose.
Route::get('managers/{id}/{parameter2}', function($id, $parameter2)
{
var_dump($id).'<br />';
var_dump($paremter2);
});
Depending on your use case, you can pass them in a query string like so: but it isn't really the 'best way', unless you're doing something like building an API that won't use the same variables in the same order all the time.
/managers?id=1¶mter2=secondParameter
var_dump(Request::query('id')).'<br />';
var_dump(Request::query('paramter2'));
I have a resource controller for member data. All of the usual resource functions, including edit, are working perfectly. I am trying to add additional edit functions within this controller so that I can create views that only are for specific subsets of the Member model data, since the data set is rather large. So, I've set up the extra routes and functions. But when I attempt to link to the edit2 resource, Laravel will not create the proper link. I don't know what I'm doing wrong. Code:
Route:
Route::get('members.edit2', array('as'=>'edit2', 'uses'=> 'MembersController#edit2'));
Route::resource('members','MembersController');
MembersController:
// Regular edit function -- works just fine:
public function edit($id)
{
$member = $this->member->find($id);
return View::make('members.edit', array(
'member'=>$member, ...
));
}
// Extra edit2 function -- should work if I could successfully route to it:
public function edit2($id)
{
$member = $this->member->find($id);
return View::make('members.edit2', array(
'member'=>$member, ...
));
}
show.blade.php:
// normal edit link (works fine, see source code below):
edit
// additional edit2 link (creates a bad link, see source code below):
edit
source code:
// normal link that uses edit for member id=27:
edit
// link that attempts to use edit2 for same member:
edit
I'm sure there is a way of doing this. It doesn't matter whether I use the named route 'edit2' rather than 'members.edit2', the exact same bad link is created. I've tried every combination I can think of. Laravel docs are not at all helpful for this. Thanks!
Your don't declare your edit2 route as you should do. Your first mistake is that the member's id you want to edit is not passed as a parameter and the second one that by calling this {{route('edit2')}} Laravel expects a url like /members.edit2 which is never going to appear. You should better use sth like /members/{id}/edit2.
Try using this:
Route::get('members/{id}/edit2', array('as'=>'edit2', 'uses'=> 'MembersController#edit2'));
and call it like:
{{ route('edit2', [$id]) }}
Also be careful, whenever you call Url::route() or simply route() you should pass their parameters in an array like:
{{route('myRoute', ['par1', 'par2', 'par3', ...]}}
I have 2 controllers, let's call them c1 and c2. Now I want to call a function in c2, Let's say actionC2, from a function in c1.
I tried something like this:
$c2_instance = new c2();
$c2_instance->actionC2();
but it won't work. I get this error: Missing argument 1 for CController::__construct().
What am I doing wrong?
EDIT: maybe its important to say that it falls on the first line
You should not call a controller from another controller . You should redirect using this
$this->redirect(array('controller/action'));
And if you do not have exactly no way other that than , reconsider your design . Solve the problem , do not try to hide it. It will bite back you anyway.
As #user488074 said, your controller must have an argument that it is looking for when you create an instance of it. Go to that controller and look at what it is looking for in the contstruct function. If you don't want to pass an argument all the time for this controller then add something like this to the construct function argument
public function foo($argument = NULL){
}
so it has a default value if you don't want to pass something.
I'm trying to achieve the following behavior: http://example.com/anything should pass anything to a default controller (namely, "category_browser"), unless anything is a controller name.
The first part is easily achieved with this line in config/routes.php:
$route[':any'] = "category_browser";
while I did not manage to get the second one to work.
I would really appreciate any input.
Other info: the number of controllers is pretty small; writing an options line for each of them is an option; they should be passed parameters.
Use this:
http://pinoytech.org/blog/post/CodeIgniter-Route-Everything-except-these-Controllers
$route['^(?!controller|controller|controller)\S*'] = "article/$1";
create a master controller that you point everything to. in the master controller, check if the set controller name exists, if so, run it, if not, call category_browser using controller name as method instead.