Kohana -- Command Line
Asked Answered
V

6

12

I'm trying to "faux-fork" a process (an email being sent via SMTP) in my web application, and the application is built on Kohana.

    $command = 'test/email';
    exec('php index.php '.$command.' > /dev/null/ &', $errors, $response);

I'm getting an error --

Notice: Undefined index: SERVER_NAME

When I look into Kohana's index.php file, I see that it is looking for a variable named SERVER_NAME, but I guess it is coming up NULL because Kohana couldn't detect this value and set it prior to run.

Any ideas how to get Kohana to run via command line?

Varied answered 25/1, 2010 at 15:47 Comment(3)
What version of Kohana are you using?Teets
you're very close in that little piece of code, just have to add --uri= in front of the $command exec('php index.php --uri='.$command.' > /dev/null &', $errors, $response); but from the rest of your question, I think it might be better to use a cronjob for it or if you use kohana3, use its HMVC-capabilitiesRotz
Now you are able to do only this: php index.php --task=demo, where this demo is saved in classes/Task/demo.php as a class Task_Demo extends Minion_Task. For more info check here kohanaframework.org/3.3/guide/minion/tasksYelenayelich
I
10

As far as I know you can't run the kohana files directly in command line because of its bootstrap methods.

You could do 2 things: export all command like functions outside kohana and run them independently.

Something else you could do is running it trough the index.php located in the kohana main folder while passing the $controller, $method variables to it so it ends up at the right object where your code is located:

For kohana 2:

php index.php controller/method/var1/var2

Kohana 3

php index.php --uri=controller/method/var1/var2

Edit: Kohana has a great CLI task runner from version 3.3 onward as official module. For version 3.2 it's still an unofficial module. I suggest you use these because they give a lot of extra options on running from CLI:

Informative answered 25/1, 2010 at 16:1 Comment(2)
This isn't true, you can run Kohana from the command line. This answer probably shouldn't be accepted.Hypostasis
This answer is both untrue and true. I actually explained that you can't run the kohana files directly. But you can run them trough the index.php like zombor beneath is saying(on of the developers). I agree that I should have phrased it differently. Edited now ^^Informative
C
18

After looking into Kohana3 source code, I found that it has support for cli (system/classes/kohana/cli.php). You can pass 3 options (uri, method, get, post). So:-

$ php index.php --uri="items/list"

would call the list method in Controller_Items.

Clostridium answered 10/3, 2010 at 4:33 Comment(1)
For anyone having the same problem as me. This feature has been removed in Kohana 3.3. You can no longer pass the uri like that. :(Fatshan
I
10

As far as I know you can't run the kohana files directly in command line because of its bootstrap methods.

You could do 2 things: export all command like functions outside kohana and run them independently.

Something else you could do is running it trough the index.php located in the kohana main folder while passing the $controller, $method variables to it so it ends up at the right object where your code is located:

For kohana 2:

php index.php controller/method/var1/var2

Kohana 3

php index.php --uri=controller/method/var1/var2

Edit: Kohana has a great CLI task runner from version 3.3 onward as official module. For version 3.2 it's still an unofficial module. I suggest you use these because they give a lot of extra options on running from CLI:

Informative answered 25/1, 2010 at 16:1 Comment(2)
This isn't true, you can run Kohana from the command line. This answer probably shouldn't be accepted.Hypostasis
This answer is both untrue and true. I actually explained that you can't run the kohana files directly. But you can run them trough the index.php like zombor beneath is saying(on of the developers). I agree that I should have phrased it differently. Edited now ^^Informative
E
7

And Kohana2 is just php index.php controller/method/param1/param2/etc

Kohana was built to run on the CLI as well as web.

Emeliaemelin answered 9/2, 2010 at 19:44 Comment(0)
O
5

If you are using Kohana 3 then you can run it from the terminal.

Example

php index.php --uri=controller/action

Options

  • --uri
  • --method
  • --get
  • --post
Orella answered 26/1, 2010 at 3:32 Comment(0)
H
2

For Kohana 3, check out these docs and source.

Hypostasis answered 12/4, 2010 at 4:11 Comment(2)
That link doesn't seem to have a valid answer or useful instructions on the process of executing Kohana 3 on CLI.Await
@Alejandro It's the only official CLI documentation I could find. Kohana is big about their source being documentation itself.Hypostasis
K
1

I had a similar issue

Did you or anyone add the SERVER_NAME to the index.php file?

If so either remove the code outside the index.php (and or bootstrap) OR you can wrap it it in a

if (PHP_SAPI === 'cli') 
{ 
   // ... 
}  else {
 //....
}
Koel answered 12/3, 2012 at 22:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.