-2

I'm writing my first Laravel package, and I need to add some lines of code (like routes to web.php, or add a function to a controller) on an event triggered by the user. How can I open the file and add some code to it?

(I know that the package's routes, views... should be published through Service Provider, but I Don't Think It'll work in this case, Because I want to Generate / Write to a file based on the user's action on view, for example, if the user wants to generate crud for a table I have to add a route to web.php of the project dynamically, and not to the one in Service provider

anas bouabid
  • 1
  • 1
  • 6
  • Are you developing the project in a local environment (localhost) ? – Ken Lee Sep 12 '21 at 20:44
  • You do not "write" a file, you must use the `service provider` to achieve this. Read the documentation related to [Package Development](https://laravel.com/docs/8.x/packages) – matiaslauriti Sep 12 '21 at 20:45
  • [This](https://laravelpackage.com/09-routing.html#routes) took me literally 5 seconds to google... It is a really good "tutorial" to follow because it has cases the user wants and can be better explained than me in an answer here ! Hope it helps. – matiaslauriti Sep 12 '21 at 21:00
  • @KenLee yes I am – anas bouabid Sep 12 '21 at 21:20
  • @matiaslauriti I think for this case Service provider wouldn't work? I want to generate/write files based on the user's actions on a view – anas bouabid Sep 12 '21 at 21:22
  • @anasbouabid explain more, as it is not clear what you want to achieve. Maybe you want to do X taking Y decision, but there is Z flow that you should that for doing that, so please explain more, be more explicit. – matiaslauriti Sep 12 '21 at 21:24

1 Answers1

-1

I just did a quick search of the docs and looks like you can do this with the Storage facade

Storage::prepend('routes/web.php', 'Whatever you wish to have at the beginning of the file');

Storage::append('file.log', 'Whatever you wish to have at the end of the file');
James
  • 15,754
  • 12
  • 73
  • 91
  • Never do this ! You must use a `service provider`. – matiaslauriti Sep 12 '21 at 20:45
  • @matiaslauriti Use a service provider to do what exactly? He wants to write to a file based on an event that occurs, not when the provider boots or registers. He could use a service provider to register events or listeners that then called some code that did the above. Unless I am missing something, this is perfectly acceptable. – James Sep 12 '21 at 20:48
  • Sure, you could go down the route of your service provider conditionally rendering routes, and that may well be a solution OP uses. OP's question is, in essence, how to append or prepend text to a file. Whether thats a PHP file or a storage file makes no material difference. Obviously, editing your routes or controllers comes with inherent risk, but nothing that a couple tests couldn't back up. – James Sep 12 '21 at 20:51
  • I understand what you are saying, but you are not correct, he is saying "I am developing a Laravel package", "I want to add some lines of code (like `routes` to `web.php` or add a method to a `controller`) on an event triggered by the user". You do not do that, you use a `service provider` to do this, you mostly never edit user files, if you are doing so, you are doing something wrong, specially with Laravel 8. – matiaslauriti Sep 12 '21 at 20:52
  • @matiaslauriti I'm not here debating which is the _best_ way to solve the problem. OP has designs on a solution and has asked a specific question to help them achieve that. Without knowing the specific use case, I, personally, believe that its rather difficult to say that moving this to a service provider is the best move. I'll leave this here, if OP wishes to continue down the path of editing the files, then the above code will do that. Perhaps you could share an answer of doing this with a service provider to offer your take? – James Sep 12 '21 at 20:55
  • Sure ! [This](https://laravelpackage.com/09-routing.html#routes) took me literally 5 seconds to google... It is a really good "tutorial" to follow because it has cases the user wants and can be better explained than me in an answer here ! Hope it helps. – matiaslauriti Sep 12 '21 at 21:00
  • 1
    Looks great. Definitely something OP should check out! – James Sep 12 '21 at 21:02
  • It's just as @James said, I want to add the route to web.php based on the user's input, I know that routes should be published through the Service provider, but as far as i know, i can't do that dynamically, for example let's say I have a crud generator and the user generated crud for a table, that means i have to create new controllers and add routes to web.php, I Don't think i can do that with Service Provider? – anas bouabid Sep 12 '21 at 21:19
  • ^ this is why I mentioned that its important to understand the use case before we recommend a different solution that what was asked for. If you're generating or "making" stuff for the user, outputting to the filesystem makes sense. – James Sep 12 '21 at 21:26
  • Yeah I'm sorry about that, I think my question was unclear – anas bouabid Sep 12 '21 at 21:28
  • @james Ther's just one problem with Storage and it's that is stores the files in storage/app, i don't think I can use it to directly edit routes file – anas bouabid Sep 12 '21 at 22:25
  • It just needs a disk to use. So you could configure a different disk, then again this is a package. `file_get_contents` and `file_put_contents` could be your friend here. – James Sep 12 '21 at 22:49